以下函數將一些單詞分解到一個數組中,調整空格並執行其他我需要的操作。我還需要刪除破折號,因爲我也將它們寫成單詞。但是這個函數不會刪除破折號。怎麼了?無法從字符串中刪除破折號( - )
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