开发者

Finding and changing the value of a single "href" value using Simple Html DOM Library

开发者 https://www.devze.com 2023-03-16 10:11 出处:网络
i need help in parsing a href attribute of a single anchore tag and then changing its value in a php file using \"simple html dom\" library.

i need help in parsing a href attribute of a single anchore tag and then changing its value in a php file using "simple html dom" library. Below is the html code:-

<li id="toggle"> <a id="open" class="open" href="#" style="display: block; "></a></li>

Now i want to get this specific href value and change it to some page like say, Logout.php depending upon if the user is logged in or not. And here is my php code:

<?php 
    include 'simple_html_dom.php';
    session_start();
    $html = new simple_html_dom();
    $html->load_file('index1.php');
     if(!isset($_SESSION['username'])){
       $ret = $html->find('li[id=hello]');
       $ret = "Hello Guest";
       $tog = $html->find('li[id=toggle]');
  开发者_运维问答     $tog = "Log In | Register";
     }else{
         $user = $_SESSION['username'];
         $ret = "Hello " . $user;
         $tog = "Log out";
         $hrf = $html->find('a[id=open]');
         $hrf->href = 'Logout.php';
      }
  ?>

Now except finding and changing the "href" value, all other things are working properly. Any help is welcomed. Thanks in advance.


Now i was able to solve the problem. i think both of we overlooked the fact that it was returning an array. So this is what i did to solve. May it help some one else also.

$e = $html->find('a[id=open]'); // returns an array containing all the info for anchor a
$link = $e[0]->href; //the value of attribute href of anchor a which was the first element       in array
$e[0]->href = 'Logout.php'; // get the new value
$link = $e[0]->href; // assign the value to a variable


You can find it all in the Manual

$tog = $html->find('li[id=toggle]');

$tog->innertext = "Log In | Register";

when you echo the content. the item will have the new value

UPDATE

You can get the href this way:

EDIT:

//$hrf = $html->find('a[id=open]');
$hrf = $html->find('#open'); // this makes more sense since it has an ID
$oldlink = $hrf->href; // this will retrieve the href value;
$hrf->href = 'Logout.php'; // new link
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号