characters 而不是 words。
<?php
$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);
//OUTPUT OF ABOVE: "In mute, remove ll cks from se bottles cellr"
?>
我需要的输出是:
分钟,从这些酒窖中取出所有软木塞
我假设我无法使用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
,仅由一个空格分隔。