The following function strips some words into an array, adjusts whitespaces and does something else I need. I also need to remove dashes, as I write them as words too. But this function doesn't remove dashes. What's wrong?
function stripwords($string)
{
// build pattern once
static $pattern = null;
if ($pattern === null) {
// pull words to remove from somewhere
$words = array('alpha', 'beta', '-');
// escape special characters
foreach ($words as &$word) {
$word = preg_quote($word, '#');
}
// combine to regex
$pattern = '#\b(' . join('|', $words) . ')\b\s*#iS';
}
$print = preg_replace($pattern, '', $string);
list($firstpart)=explode('+', $print);
return $firstpart;
}
To answer your question, the problem is the
\b
which designates a word boundary. If you have a space before or after the hyphen, it won't remove it as in " - ", the word boundary doesn't apply.From http://www.regular-expressions.info/wordboundaries.html:
A simple solution:
By adding
\s
along with\b
to your pattern and using a positive look-behind and a positive look-ahead, you should be able to solve your problem.Nowhere in your regex pattern are you looking for dashes. Why not just do
after you do your regex stuff?