I'm trying to use preg_replace to replace the contents between 2 tags {code} and {/code}. Like this $lCode = preg_replace("/\[code\].*\[\/code\] /", "Changed", $aText); PHP: However if there are 2 or more sets of the tags it only does the replace between the first opening {code} and last closing {/code} but I want it to replace each {code} blah blah {/code} separately. Hope I've explained this in an understandable way, any help much appreciated.
Had to do $lCode = preg_replace("/\[code\].*?\[\/code\]/", "Changed", $aText); PHP: That question mark cost me much hair and time.
You could also have told the expression to be ungreedy - by default the pattern will match the largest amount of text that it can, so it would grab the first instance of {code} and the last instance if {/code}, ignoring any {/code} entries between the two. To make a pattern ungreedy, use the U flag, so $lCode = preg_replace("/\[code\].*\[\/code\] /U", "Changed", $aText); PHP: Hope it helps for next time John