texelate Posted November 10, 2006 Share Posted November 10, 2006 HiI'd like to perform a str_replace on some HTML but I don't want any of the HTML tages to be affected. For example if the original string has:[code]$html = '<div><img src="/a_picture_of_me.jpg" alt="" /> A picture of me</div>';[/code]And I do a str_replace to change "picture" to "Jon" I will end up with:[code]$html = '<div><img src="/a_picture_of_Jon.jpg" alt="" /> A picture of Jon</div>';[/code]Is there any way I can replace it without affecting any HTML tags - so I'd end up with:[code]$html = '<div><img src="/a_picture_of_me.jpg" alt="" /> A picture of Jon</div>';[/code]Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/26820-replace-text-with-altering-html/ Share on other sites More sharing options...
Nicklas Posted November 10, 2006 Share Posted November 10, 2006 If the word "me" is always followed by a < tag, then you could do like this:[code=php:0]echo preg_replace('/\bme\b(?=<)/s', 'Jon', $html);[/code] Quote Link to comment https://forums.phpfreaks.com/topic/26820-replace-text-with-altering-html/#findComment-122654 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.