代码:
$testing="sniper this and sniper that";
$connectors=array('and', 'now', 'but', 'still', 'so', 'only', 'therefore', 'moreover', 'besides', 'consequently','nevertheless', 'for', 'however', 'hence', 'either', 'or', 'neither', 'nor', 'both', 'also','while', 'then', 'who', 'which', 'that', 'although', 'though', 'since', 'until', 'as', 'if','after', 'before', 'how', 'once', 'when', 'lest', 'why', 'unless', 'because', 'till', 'where', 'whether','the', 'a', 'an','about', 'above', 'across', 'after', 'against', 'along', 'among', 'around', 'at', 'before', 'behind','below', 'beneath', 'beside', 'between', 'beyond', 'but', 'by', 'despite', 'down', 'during', 'except','for', 'from', 'in', 'inside', 'into', 'like', 'near', 'of', 'off', 'on', 'onto', 'out', 'outside', 'over',
'past', 'since', 'through', 'throughout', 'till', 'to', 'toward', 'under', 'underneath', 'until', 'up', 'upon','with', 'within', 'without','who', 'whom', 'whose', 'which', 'that', 'this', 'these', 'those', 'I', 'Me', 'You', 'He', 'him', 'she','her', 'it', 'we', 'us', 'they', 'them', 'Mine', 'Yours', 'His', 'Hers', 'Its', 'Ours', 'Theirs', 'whoever','whomever', 'whichever', 'myself', 'yourself', 'himself', 'herself', 'itself','ourselves', 'yourselves', 'themselves');
foreach($connectors as $word)
{
$pattern="/".strtolower($word)."/";
$data=preg_replace($pattern,"/ /", $testing);
}
echo $data."<br>";
echo $testing;
输出:
狙击这个和狙击手
狙击这个和狙击手
预期产出:
狙击这个和狙击手
狙击手狙击手
我做错了什么?
答案 0 :(得分:3)
对于初学者,您在每次迭代时都会覆盖$data
。
答案 1 :(得分:1)
为什么不使用:
$testing = ......;
$connectors = array( ...... );
$data = str_ireplace( $connectors, " ", $testing);
echo $data."<br />".$testing;
答案 2 :(得分:1)
尝试:
$testing = "sniper this and sniper that";
$connectors = array( /* your data */ );
$output = implode(' ', array_diff(explode(' ', $testing), $connectors));
不区分大小写的方式:
$output = implode(' ', array_udiff(explode(' ', $testing), $connectors, 'strcasecmp'));