views:

56

answers:

3

Hi, I have this code on my page, but the link has different names and ids:

   <div class="myclass">
    <a href="http://www.example.com/?vstid=00575000&amp;amp;veranstaltung=http://www.example.com/page.html"&gt;
Example Text</a>
    </div>

how can I remove and Replace it to this:

<div class="myclass">Sorry no link</div>

With PHP or Javascript? I tried it with str.replace

Thank you!

+4  A: 

I assume you mean dynamically? You won't be able to do this with php because it is server side, and doesn't have anything to do with the HTML once its been output to the screen.

See: http://www.tizag.com/javascriptT/javascript-innerHTML.php for the javascript.

Or you could use jquery which is just better and nicer than trying to do a cross browser compatible javascript script.

$('.myclass').html('Sorry...');

Thomas Clayson
+1  A: 

If the page is still on the server before you need to make the replacement, do this:

<?php if (allowed_to_see_link()) { ?>
<div class="myclass">
<a href="http://www.example.com/?   vstid=00575000&amp;veranstaltung=http://www.example.com/page.html"&gt;
 Example Text</a>
</div>
<?php } else { ?>
non-link-text
 <php } ?>

and also write the named functions...

Ian
A: 

You might want to clearify what you are up to. If that is your file, then you can simply open up in an editor and remove the portions. If you want to modify HTML with PHP, you can use native DOM

$dom = new DOMDocument;
$dom->loadHTML($htmlString);
$xPath = new DOMXPath($dom);
foreach( $xPath->query('//div[@class="myclass"]/a') as $link) {
    $link->parentNode->replaceChild(new DOMText('Sorry no link'), $link);
}
echo $dom->saveHTML();

The above code would replace any direct <a> element children of any <div> elements that have a class attribute of myclass with the Textnode "Sorry no link".

Gordon