Jump to content

pre_replace() within HTML


gerkintrigg

Recommended Posts

I have the following code:

<?php
$html = '<b alt="bum">The word "bum" needs replacing with something less offensive.</b>';
$pattern = '@\>(.+?)\<@';

$replacement = '>'.str_replace('bum','bottom',$html).'<';

$page = preg_replace($pattern, $replacement, $html);

echo $page;
?>

 

If you run it, you'll find that the word "bum" is replaced with "bottom" which is what I want, but the original alt tag (and I know it's nonsense HTML) is also being duplicated, with the duplicate being replaced... though I don't know why it's showing twice...

 

I just want to keep the original HTML but replace its contents... any suggestions?

Link to comment
https://forums.phpfreaks.com/topic/181488-pre_replace-within-html/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.