0

I need an if() function to do this:

preg_replace() letters (a, b, c, etc.) except for those wrapped in tags (<p>, <b>, <span>, etc.) and exclude the letters if they are part of a certain word.

$string = "<p>replace everything inside tags <b>only</b> </p>exception";  
$patterns = array();  
$patterns[0] = '/e/';  
$patterns[1] = '/b/';  
$patterns[2] = '/s/';  
$replacements = array();  
$replacements[2] = '-e-';  
$replacements[1] = '-b-';  
$replacements[0] = '-s-';  
echo preg_replace($patterns, $replacements, $string);

I want "<p>", "<b>" and the word "exception" to remain unaltered.

Gumbo
  • 643,351
  • 109
  • 780
  • 844
Zebra
  • 3,858
  • 9
  • 39
  • 52

1 Answers1

1

This is almost always a bad idea to try to do in regex. You should try to use an HTML parser instead:

Robust and Mature HTML Parser for PHP

Community
  • 1
  • 1
Jeff Winkworth
  • 4,874
  • 6
  • 34
  • 33