开发者

PHP or Javascript: Simply Remove and Replace HTML Code

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

   <div cla开发者_StackOverflowss="myclass">
    <a href="http://www.example.com/?vstid=00575000&amp;veranstaltung=http://www.example.com/page.html">
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!


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...');


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">
 Example Text</a>
</div>
<?php } else { ?>
non-link-text
 <php } ?>

and also write the named functions...


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".

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜