我正在尝试使用preg_replace.Content in text 从文本文件中删除一些内容,如下所示1 *useless*Text Steve Waugh如何转换内容1 Steve Waugh我的代码在下面。它不起作用。我不知道我在哪里犯了错误?$content = file_get_contents('test.txt');$content = preg_replace('/\s[*]useless[*]Text\s/', ' ', $content);file_put_contents('test.txt', $content);
1 回答

梦里花落0921
TA贡献1772条经验 获得超6个赞
如果您想保持缩进之前*useless*Text
,使其Steve Waugh
在结果之前,您可以使用捕获组。
\h*\R(\h+)[*]useless[*]Text\s*
\h*\R
匹配 0+ 个水平空白字符和一个 Unicode 换行符序列(\h+)
捕获组 1,匹配 1+ 水平空白字符[*]useless[*]
匹配*useless*
Text\s*
匹配Text
和 0+ 个空白字符
在更换使用$1
preg_replace('/\h*\R(\h+)[*]useless[*]Text\s*/', '$1', $content);
要替换为预定的空格,您可以使用
\s*[*]useless[*]Text\s*
preg_replace('/\s*[*]useless[*]Text\s*/', ' ', $content);
- 1 回答
- 0 关注
- 137 浏览
添加回答
举报
0/150
提交
取消