Reputation: 875
Let's say I have block of HTML where I want to do a find and replace on the content before echoing out to the screen.
How do I ensure I don't change the HTML but just change the content (in PHP).
For example, if I had this:
<div class='Hello'>Hello</div>
and I wanted to replace all the 'Hello' words in the content with 'Hi' I would want to get this result:
<div class='Hello'>Hi</div>
At the moment I am using preg_replace, e.g.
$new_code = preg_replace(array("/Hello/"), array("Hi"), $code);
But this would give me the result
<div class='Hi'>Hi</div>
Upvotes: 0
Views: 193
Reputation: 26
Don't use regex to find text in HTML use a DOM parser instead:
You could use DomDocument but be careful it will do strange things with partial HTML, but here's an example to get you going:
$dom = new DOMDocument();
$dom->loadHTML("<html><body><p>Hello</p></body></html>");
$els = $dom->getElementsByTagName('*');
foreach ( $els as $el ) {
if (preg_replace("/Hello/","Hi",$el->nodeValue))
{
$el->nodeValue =preg_replace("/Hello/","Hi",$el->nodeValue);
}
}
echo $dom->saveHTML();
or Simple HTML DOM may meet your needs:
Example from site:
$html = str_get_html('<div class='Hello'>Hello</div>');
$html->find('div', 0)->innertext = 'Hi';
echo $html;
Upvotes: 1
Reputation: 2691
Try this:
<?PHP
$html = '<div class="HeLLo" id="HEllO">Hello how to say "hEllo"
for heLlo world in a helLo world of hellO</div>';
echo preg_replace('/(hello)(?![^>](\s*\w+=".*")*\s*>)/i', "Hi", $html);
?>
Upvotes: 0
Reputation: 702
Try using this assertion (see the answer to this question) to only match text that occurs between >
and <
:
$new_code = preg_replace(array("/(Hello)(?=[^>]*(<|$))/"), array("Hi"), $code);
Upvotes: 0
Reputation: 5792
If you are looking forward to change from Hello
to Hi
then,
$new_code = preg_replace(array("/\>Hello/"), array(">Hi"), $code);
Upvotes: 0