下面的代码的问题在于它从字符串中删除了 characters 而不是 words。
$str = "In a minute, remove all of the corks from these bottles in the cellar";
$useless_words = array("the", "of", "or", "in", "a");
$newstr = str_replace($useless_words, "", $str);
以上输出:
In mute, remove ll cks from se bottles cellr
我需要的输出是:
minute, remove all corks from these bottles cellar
我假设我无法使用
str_replace()
。我可以做什么来实现这个目标?
preg_replace将完成这项工作:
$str = "The game start in a minute, remove all of the corks from these bottles in the cellar";
$useless_words = array("the", "of", "or", "in", "a");
$pattern = '/\h+(?:' . implode($useless_words, '|') . ')\b/i';
$newstr = preg_replace($pattern, "", $str);
echo $newstr,"\n";
输出:
The game start minute, remove all corks from these bottles cellar
说明:
图案看起来像:
/\h+(?:the|of|or|in|a)\b/i
/ : regex delimiter
\h+ : 1 or more horizontal spaces
(?: : start non capture group
the|of|or|in|a : alternatives for all the useless words
) : end group
\b : word boundary, make sure we don't have a word character before
/i : regex delimiter, case insensitive
$useless_words = array(" the ", " of ", " or ", " in ", " a ");
$str = "In a minute, remove all of the corks from these bottles in the
cellar";
$newstr = str_replace($useless_words, " ", $str);
$trimmed_useless_words = array_map('trim',$useless_words);
$newstr2 = '';
foreach ($trimmed_useless_words as &$value) {
if (strcmp($value, substr($newstr,0,strlen($value)))){
$newstr2 = substr($newstr, strlen($value) );
break;
}
}
if ($newstr2 == ''){
$newstr2 = $newstr;
}
echo $newstr2;
除了强制整个单词与单词边界匹配之外,还可以使用条件表达式来消耗前导或尾随空白字符(但不能同时使用两者!)。
代码:(演示)
$str = "The game start in a minute, remove all of the corks from these bottles in the cellar they're in";
$useless_words = ["the", "of", "or", "in", "a"];
var_dump(
preg_replace('/(\s+)?\b(?:' . implode('|', $useless_words) . ')\b(?(1)|\s+)/i', '', $str)
);
输出:
string(69) "game start minute, remove all corks from these bottles cellar they're"
请注意,前导
The
加上尾随空格被删除,最后一个 in
及其前导空格被删除,all of the corks
变成了 all corks
,仅由一个空格分隔。