I have a HTML string like this:
$text = "<strong>text</strong>more text<em>more text</em>additional text<span>text</span>....."
I would like to replace the x
characters with ?
but only inside the <em>
tag.
The string above will become:
$text = "<strong>text</strong>more text<em>more te?t</em>additional text<span>text</span>....."
How can it be done in PHP?
2
Answers
One option is to use the DOM extension. It’s convoluted and documentation could use more examples, but it gets the job done:
Demo
Note my example assumes that you don’t have other tags inside.
As Álvaro ‘s answer works out pretty well, there might be an easier solution using XPath.
What does the code snippet?
<em>
elements whose content contains the letter "x". The result of that will be eithernull
or a DOMNodeList instance.The result
The result is a clean string with replaced "x" letters in the text nodes of
<em>
elements.<strong>text</strong>more text<em>more te?t</em>additional text<span>text</span>
Why using XPath instead of using RegEx
The answer is simple: Because it ‘s HTML. It ‘s recommended to parse and manipulate XML / HTML documents with PHP ‘s own DOM library because it ‘s way faster than using regular expressions and it is made for this kind of DOM actions. In the shown code example we ‘re using only one iteration. Even this iteration is pretty fast, because it ‘s using an internal iterator instead of a simple array. The difference is, that an array occupies the memory exponentially with its entire content. An iterator only occupies the memory for the current element in the iteration. Long story short: More Performance. Less memory consumption.