Re: Regular expression help
| From: | John Holmes | Date: | Thu, 09 Sep 2004 13:27:32 +0000 |
| Subject: | Re: Regular expression help | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-196211@lists.php.net to get a copy of this message | ||
From: "Skippy" <skippy@zuavra.net>
I'm trying to replace all occurances of the X character in a text with Y, but only those X's that occur between bold tags (<b></b>).<?php $str = 'This X and this X will not be fixed, but <b>this X and</b> and hopefully this <b>X</b> should be, along <b> with this X also. </b>, but not this X.'; function myreplace($match) { $from = 'X'; $to = 'Y'; return str_replace($from,$to,$match[0]); } $new_str = preg_replace_callback('#<b>.*</b>#Uis','myreplace',$str); echo $new_str; ?>