Forum Moderators: coopster

Message Too Old, No Replies

Remove word from string

PHP word remove issue

         

tabish

6:27 pm on Jan 5, 2007 (gmt 0)

10+ Year Member



hello all,

I got this function from somewhere else:

$var = "somesomesome and in somesomei I somie, some. someone .some";
$badword = "some";
$var = preg_replace(array("/([\W]+){$badword}([\W]+)/","/([\W]+){$badword}([\W]+)/"),$1?.str_repeat("*",strlen($badword)).$2?,$var);

This is to remove word from a string but that word SHOULD not be a part of any word.. for example if i want to remove "some" then "someone" should be left as it is..

BUT this function is not working properly.. can anybody take a look and tell me what is wrong?

I am REALLY bad in regular expressions..

Thank you so much..

alfaguru

6:50 pm on Jan 5, 2007 (gmt 0)

10+ Year Member



That expression does not cope with the bad word being at the beginning of the string, for a start.

I assume you want to replace it with a string consisting of asterisks of the same length, is that right? Like this:


$len = strlen($badword);
$var = preg_replace("/\b$badword\b/", str_repeat('*',$len), $var);