Php 从句子中删除除所选单词以外的单词
我有大约1000个不同的句子。我想从这些句子中删除“DLC”一词,但“All DLC BG”和“DLC Comfort”两个片段除外,因此不应从这两个片段中删除“DLC”一词 我认为这里需要Php 从句子中删除除所选单词以外的单词,php,Php,我有大约1000个不同的句子。我想从这些句子中删除“DLC”一词,但“All DLC BG”和“DLC Comfort”两个片段除外,因此不应从这两个片段中删除“DLC”一词 我认为这里需要array(),但我不知道怎么做 我试过这样的方法: if (stripos($title, 'All DLC BG') && stripos($title, 'DLC Comfort') == false) { $title = str_ireplace("DLC "
array()
,但我不知道怎么做
我试过这样的方法:
if (stripos($title, 'All DLC BG') && stripos($title, 'DLC Comfort') == false) {
$title = str_ireplace("DLC ", " ", $title);
}
但是不起作用。我想您可以使用
str\u replace()
并传递一个空字符串。
希望这有帮助 您就快到了,但是您需要检查这两个操作的布尔条件,并使用三重等于:
if (stripos($title, 'All DLC BG') === false && stripos($title, 'DLC Comfort') === false) {
$title = str_ireplace("DLC ", "", $title);
}
另外,我认为您希望替换为空字符串,而不是一个空格。如果一个句子中可以多次出现
DLC
,一个选项可能是使用正则表达式,使用您不想更改的替换选择那些句子,然后跳过那些使用的。然后仅使用单词边界匹配\bDLC\b
在替换中,请使用空字符串
\b(?:All DLC BG|DLC Comfort)\b(*SKIP)(*FAIL)|\bDLC\b
见
例如:
$re = '/(?:All DLC BG|DLC Comfort)(*SKIP)(*FAIL)|\bDLC\b/m';
$title = 'test All DLC BG test DLC Comfort and DLC here
test here All DLC BG All DLC BG BG ALL DLC';
$result = preg_replace($re, '', $title);
echo $result;