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 ^