Can't remove dashes (-) from string
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开发者_JS百科 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:
There are three different positions that qualify as word boundaries:
- Before the first character in the string, if the first character is a word character.
- After the last character in the string, if the last character is a word character.
- Between two characters in the string, where one is a word character and the other is not a word character.
A "word character" is a character that can be used to form words.
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.
$pattern = '#(?<=\b|\s|\A)(' . join('|', $words) . ')(?=\b|\s|\Z)\s*#iS';
Nowhere in your regex pattern are you looking for dashes. Why not just do
$string = str_replace('-', '', $string);
after you do your regex stuff?
精彩评论