I would like to replace words outside of HTML-tags.
So if I got
<a href="test.html" title="Hello">Hello</a>
and I want to replace "Hello" with "Bye" I would like to get this result:
<a href="test.html" title="Hello">Bye</a>.
Well, I learned that I have to use a DOM-parser to achieve that.
So I used https://github.com/sunra/php-simple-html-dom-parser and included it.
Now I did
$test = $dom->find('text');
To get the text of the dom.
Now I can loop through the results:
foreach($test as $t) {
if (strpos($t->innertext,$word)!==false) {
$t->innertext = preg_replace(
'/' . preg_quote( $word, "/" ) . '/i',
"<a href='$url' target='$target' data-uk-tooltip title='$item->title'>$0</a>",
$t->innertext,1
);
}
}
But unfortunately, if $item->title
contains $word, the HTML-structure is smashed.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…