php - replace string but not a specific word that contains that string -


suppose have string

$string = 'this gal_s, not 1_s not 2_s not 3_s'; $find = '_s'; $replace = ''; 

i woul return

"this gal_s, not 1 not 2 not 3" 

so word gal_s not affected

is possible?

you can use preg_replace negative lookbehind this:

$repl = preg_replace('/(?<!\bgal)_s/', '', $str); 

live demo: http://ideone.com/getsv3


Comments

Popular posts from this blog

python - How to create a legend for 3D bar in matplotlib? -

java - Multi-Label Document Classification -

php - Dynamic url re-writing using htaccess -