Page 1 of 1

Using preg_replace() to remove words from a string.

Posted: Tue May 18, 2004 3:09 pm
by hawleyjr
I am trying to remove whole words from a string. (I need to remove the word 'and' but not the substring 'and' of the word andy.) The below works but only 'and' is removed. Nothing else in the array $a_words_to_exclude is removed from the string.

Code: Select all

<?php
$searchVal = 'Additional and coverages - grey otto unavailable options andy and on some';

$a_words_to_exclude  = array('and','a','or','for','at','an', 'the', 'in', 'of','etc','is','to','-','if','do','on');

$needle = join('|',$a_words_to_exclude);
$searchVal = preg_replace("/\b($needle)\b/i","",$searchVal,-1);


//I have also tried:
//$searchVal = preg_replace("/($needle)/i","",$searchVal,-1);
?>
I would like $searchVal to return the following:

'Additional coverages grey otto unavailable options andy some'

Posted: Tue May 18, 2004 3:33 pm
by leenoble_uk
It seems to be the hyphen causing the problem.
I think that since - is not a word character then \b does not apply.
However \W does apply in this instance.

I tried this and I can get it to do what you want but I'm not sure if you will have issues with these words where they appear at the end of a sentence.

Code: Select all

<?php
$searchVal = 'Additional and coverages - grey otto at unavailable in of options andy and on some';

$a_words_to_exclude = array('and','a','or','for','at','an', 'the', 'in', 'of','etc','is','to','if','on','do','-');

$needle = join("|",$a_words_to_exclude);

$expression = "/(\b|\W)($needle)(\b|\W)/i";
$searchVal = preg_replace($expression," ",$searchVal);

echo $expression. "<br>";
echo $searchVal;

?>

Posted: Tue May 18, 2004 4:35 pm
by hawleyjr
Works great, thanks...