Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/239.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用preg_replace函数时出错_Php_Html - Fatal编程技术网

Php 使用preg_replace函数时出错

Php 使用preg_replace函数时出错,php,html,Php,Html,我只想换一个 <span class="google-src-text" style="direction: ltr; text-align: left">any character</span> 任意字符 在这个源代码中,一行一行地使用空格,我的php代码是 $content = file_get_contents('path/to/html.html'); $content = str_replace('>', ">\n", $content); e

我只想换一个

<span class="google-src-text" style="direction: ltr; text-align: left">any character</span>
任意字符
在这个源代码中,一行一行地使用空格,我的php代码是

$content = file_get_contents('path/to/html.html');
$content = str_replace('>', ">\n", $content);

echo preg_replace('/<span class="google-src-text" style="direction: ltr; text-align: left">.*.<\/span>/', ' ', $content);
$content=file_get_contents('path/to/html.html');
$content=str_replace(“>”、“>\n”、$content);
echo preg_替换('/.../','$content);

但此代码将替换所有使用
和最后一个

的内容。如果“任意字符”中没有HTML,则此代码有效

/([^
*
默认情况下是贪婪的,您需要将其更改为lazy,如下所示:

preg_replace('/<span class="google-src-text" style="direction: ltr; text-align: left">.*?<\/span>/', ' ', $content);
//                                                               Note the question mark ^
preg_replace(“/.*?/”,“$content”);
//注意问号^
这将匹配*到第一个
,请注意,如果内部有嵌套的跨距,它将不会一直提取到末尾

这就是为什么应该使用

preg_replace('/<span class="google-src-text" style="direction: ltr; text-align: left">.*?<\/span>/', ' ', $content);
//                                                               Note the question mark ^