jmace Posted November 19, 2010 Share Posted November 19, 2010 I was wonder how I can use preg_replace() to change a string like this: Sponge Face Painting Applicators Medium Sponge Applicator Into a string like this: Face Painting Medium Sponge Applicator In other words, because the word "Sponge" is in the string twice, and "Applicator" is in the string twice (except that the first ends in an s) they would be gone. I've tried stuff like: $title = preg_replace('/(?:\W|^)(\w*?)(?:\W.*?)(?=\1)/',"",$title); But it's way off and gives me: Sponge Applicator Any help would be great! Quote Link to comment Share on other sites More sharing options...
sasa Posted November 21, 2010 Share Posted November 21, 2010 try <?php $test = 'Sponge Face Painting Applicators Medium Sponge Applicator'; $c = 1; while ($c) $test = preg_replace('/^(.*?)(\b[^ ]+?)s?\b (.*\b\2s?\b.*$)/', '\1\3', $test,-1,$c); echo $test; ?> Quote Link to comment Share on other sites More sharing options...
jmace Posted November 22, 2010 Author Share Posted November 22, 2010 Thanks you so much for the example! That works perfectly. I owe you one. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.