I’m trying to use the simple_html_dom php class to create a find and replace function that looks for keywords and replace them by a link to a definition of the keyword, with the keyword as link text.
How can i find and replace “Dexia” with <a href="info.php?tag=dexia">Dexia</a> using this class, inside a string such as <div><p>The CEO of the Dexia bank has just decided to retire.</p></div> ?
That’s somewhat tricky, but you could do it this way:
I’ve added an emphasis element just to illustrate that it works with inline elements too.
Setup
The interesting thing above is the XPath of course. It queries the loaded DOM for all
DOMTextnodes containing the needle "Dexia". The result isDOMNodeList(as usual).The replacement
The found
$nodewill contain the string The CEO of the Dexia bank forwholeText, despite it being inside thePelement. That is because the$nodehas a siblingDOMElementwith the emphasis after bank. I am creating the link as a string instead of a node and replace all occurences of "Dexia" (regardless of word boundary – that would be a good call for Regex) in thewholeTextwith it. Then I create aDocumentFragmentfrom the resulting string and replace theDOMTextnode with it.W3C vs PHP
Using
DocumentFragement::applyXML()is a non-standard approach, because the method is not part of the W3C DOM Specs.If you would want to do the replacement with the standard API, you’d first have to create the
AElement as a newDOMElement. Then you’d have to find the offset of "Dexia" in thenodeValueof theDOMTextand split theDOMTextNode into two nodes at that position. Remove Dexia from the returned sibling and insert the Link Element, before the second one. Repeat this procedure with the sibling node until no more Dexia strings are found in the node. Here is how to do it for one occurence of Dexia:And finally the output