Hem
Hem

Reputation: 71

Finding and changing the value of a single "href" value 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.

Upvotes: 2

Views: 3137

Answers (2)

Ibu
Ibu

Reputation: 43810

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

Upvotes: 1

Hem
Hem

Reputation: 71

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

Upvotes: 1

Related Questions