我正在尝试删除特定的单词。
$data = str_replace( $wordsToRemove, '!', $data );
但这会留下某些字符。因此,例如,如果测试是一个要删除的词,那么现在测试ing,并且测试变成!s
所以我正试图摆脱这样的:
$data = preg_replace("/!anyAmountofCharsRemovedUntilSingleSpace /", ' ', $data);
这样做对吗?
试试这个:
<?php
$words = 'Hello there this is some sample text';
$replaced = preg_replace('/th.*? /','',$words);
echo $replaced;
?>
输出:
你好,是的一些示例文本
编辑
<?php
$words = 'Hello there this is some sample text';
$chars = array(
'the',
'so',
'sa'
);
for($i = 0; $i < count($chars); $i++)
$words = preg_replace('/'.$chars[$i].'.*? /','',$words);
echo $words;
?>
输出:
你好,这是文字
如果要删除单词,为什么要用"!"替换它们?
你可以简单地写
$data = str_replace( $wordsToRemove, '', $data );
其将用null替换这些单词。