[英]preg_replace remove every char from ! until a single space is found
我正在尝试删除特定的单词。
$data = str_replace( $wordsToRemove, '!', $data );
但是,这会使某些字符失去言语。 因此,例如,如果test是一个要删除的词,则测试现在!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替换单词。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.