I would like to replace ">" with ">" and "<" with "<" but only when they occur outside "<pre>" and "</pre>". Is this possible?
$newText = preg_replace('>', '>', $text);
I would be using the preg_replace in PHP as above.
I would like to replace ">" with ">" and "<" with "<" but only when they occur outside "<pre>" and "</pre>". Is this possible?
$newText = preg_replace('>', '>', $text);
I would be using the preg_replace in PHP as above.
If you want to do this with a regex, the trick is to make your regex match the things you don't want to replace as well as the things you want to replace, and dynamically calculate the replacement depending on what was matched.
$new_text = preg_replace_callback('%<|>|<pre>.*?</pre>%si', compute_replacement, $text);
function compute_replacement($groups) {
if ($groups[0] == '<') {
return '<';
} elseif ($groups[1] == '>') {
return '>';
} else {
return $groups[0];
}
}