PHP or [removed] Simply Remove and Replace HTML Co

2019-08-02 05:35发布

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;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!

3条回答
\"骚年 ilove
2楼-- · 2019-08-02 06:02

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

查看更多
甜甜的少女心
3楼-- · 2019-08-02 06:18

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

查看更多
唯我独甜
4楼-- · 2019-08-02 06:26

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

查看更多
登录 后发表回答