Quoting John Holmes <[EMAIL PROTECTED]>: > From: "Skippy" <[EMAIL PROTECTED]> > > 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; > > ?>
It works great, thank you. I had tried something on my own previously, but without the ungreedy modifier and with replacements inside a repeated while{} cycle instead of a callback. The main drawback in my version was that an X near "hopefully" would have been replaced as well (since technically it is between <b> and </b>). But that wasn't what I wanted, of course. -- Romanian Web Developers - http://ROWD.ORG -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php