以下函数将一些单词分解到一个数组中,调整空格并执行其他我需要的操作。我还需要删除破折号,因为我也将它们写成单词。但是这个函数不会删除破折号。怎么了?无法从字符串中删除破折号( - )
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;
}
'$ pattern'看起来像什么? –
它是一种宣传,而不是破折号。破折号是: - – Buddy
你可以举一个不能按预期工作的例子吗? – Gumbo