How can I search and replace a specific string (text + html tags) in a web page using the native PHP DOM Parser?
For example, search for
<p> <a href="google.com"> Check this site </a> </p>
This string is somewhere inside inside an html tree.
I would like to find it and replace it with another string. For example,
<span class="highligher"><p> <a href="google.com"> Check this site </a> </p></span>
Bear in mind that there is no ID to the <p>
or <a>
nodes. There can be many of those identical nodes, holding different pieces of text.
I tried str_replace, however it fails with complex html markup, so I have turned to HTML Parsers now.
EDIT:
The string to be found and replaced might contain a variety of HTML tags, like divs, headlines, bolds etc.. So, I am looking for a solution that can construct a regex or DOM xpath query depending on the contents of the string being searched.
Thanks!
`?
– Audite Marlow Nov 11 '15 at 10:48elements, how do you identify the right one and replace it ?
– user3857924 Nov 11 '15 at 11:06