tags:

views:

66

answers:

1

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.

+1  A: 

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

http://stackoverflow.com/questions/292926/robust-mature-html-parser-for-php

Jeff Winkworth