php preg_replace 替换文本文件中的 space
php preg_replace replace space in text file
我正在尝试使用 preg_replace
从文本文件中删除一些内容。文本中的内容如下
1
*useless*Text
Steve Waugh
如何转换像
这样的内容
1 Steve Waugh
我的代码是 below.It 不是 work.I 不知道我哪里出错了?
$content = file_get_contents('test.txt');
$content = preg_replace('/\s[*]useless[*]Text\s/', ' ', $content);
file_put_contents('test.txt', $content);
如果您想在 *useless*Text
之前保持缩进,以便在结果中位于 Steve Waugh
之前,您可以使用捕获组。
\h*\R(\h+)[*]useless[*]Text\s*
\h*\R
匹配 0+ 个水平空白字符和一个 Unicode 换行符序列
(\h+)
捕获 组 1,匹配 1+ 个水平空白字符
[*]useless[*]
匹配 *useless*
Text\s*
匹配 Text
和 0+ 个空白字符
看到一个regex demo | Php demo
在替换中使用</code></p>
<pre><code>preg_replace('/\h*\R(\h+)[*]useless[*]Text\s*/', '', $content);
要替换预定的空格,可以使用
\s*[*]useless[*]Text\s*
preg_replace('/\s*[*]useless[*]Text\s*/', ' ', $content);
我正在尝试使用 preg_replace
从文本文件中删除一些内容。文本中的内容如下
1
*useless*Text
Steve Waugh
如何转换像
这样的内容1 Steve Waugh
我的代码是 below.It 不是 work.I 不知道我哪里出错了?
$content = file_get_contents('test.txt');
$content = preg_replace('/\s[*]useless[*]Text\s/', ' ', $content);
file_put_contents('test.txt', $content);
如果您想在 *useless*Text
之前保持缩进,以便在结果中位于 Steve Waugh
之前,您可以使用捕获组。
\h*\R(\h+)[*]useless[*]Text\s*
\h*\R
匹配 0+ 个水平空白字符和一个 Unicode 换行符序列(\h+)
捕获 组 1,匹配 1+ 个水平空白字符[*]useless[*]
匹配*useless*
Text\s*
匹配Text
和 0+ 个空白字符
看到一个regex demo | Php demo
在替换中使用</code></p>
<pre><code>preg_replace('/\h*\R(\h+)[*]useless[*]Text\s*/', '', $content);
要替换预定的空格,可以使用
\s*[*]useless[*]Text\s*
preg_replace('/\s*[*]useless[*]Text\s*/', ' ', $content);