Possible Duplicate:
RegEx match open tags except XHTML self-contained tags
How to parse and process HTML with PHP?
Simple: How to replace “all between” with php?
I am looking for a way to change all the text in an anchor tag that contains certain text in php
ie:
<a href="stackoverflow.com"><strong>This is text</strong></a>
I want to seach for 'This is text' and then replace the anchor tags that the text is in with with something else.
Is it easiest to do with regular expressions?
Thanks
Html can be treated like xml. You should be using the php xml functions to do this. See http://php.net/manual/en/book.xml.php
You can do this easily with DOMXpath
:
$s =<<<EOM
<a href="stackoverflow.com"><strong>This is text</strong></a>
EOM;
$d = new DOMDocument;
$d->loadHTML($s);
$x = new DOMXPath($d);
foreach ($x->query("//a[contains(strong, 'This is text')]") as $node) {
$new = $d->createTextNode('Hello world');
$node->parentNode->replaceChild($new, $node);
}
echo $d->saveHTML();