Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/275.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正则表达式-模式不匹配_Php_Regex_Preg Replace - Fatal编程技术网

PHP正则表达式-模式不匹配

PHP正则表达式-模式不匹配,php,regex,preg-replace,Php,Regex,Preg Replace,我试图用一个自定义类替换所有的body和html选择器。(稍后html内容将显示在自定义容器中,可用的css不会影响主站点。) 以下是我尝试过的一个例子: $data = "html body { color: red; }\nbody html { color: red; }\nhtml, body { color: red; }\nbody, html, .body table, #html-TemplateContainer {\nbackground-color:#eee;\n}"; e

我试图用一个自定义类替换所有的body和html选择器。(稍后html内容将显示在自定义容器中,可用的css不会影响主站点。)

以下是我尝试过的一个例子:

$data = "html body { color: red; }\nbody html { color: red; }\nhtml, body { color: red; }\nbody, html, .body table, #html-TemplateContainer {\nbackground-color:#eee;\n}";
echo "<pre>".preg_replace('/(^|[,}\s]+)(body html|html body|html|body)([,.{\s]*)/is', '$1'.'.foo'.'$3', $data)."</pre>";
我不明白为什么第3行和第4行的主体和html(scnd选择器)没有被替换,据我所知,它应该与正则表达式匹配

你应该试试这个。。。
$data = "html body { color: red; }\nhtml *.class { color: red; }\nhtml,body { color: red; }\nhtml[dir=\"rtl\"], body, .body table, #html-container\n{\n    background-color:#eee;\n}";
echo "<pre>".preg_replace('/(?<=^|[,}\]\s])(\*\s+html\s+body|\*\s+body\s+html|html\s+\*\s+body|html\s+body\s+\*|body\s+\*\s+html|body\s+html\s+\*|\*\s+html|\*\s+body|html\s+\*|html\s+body|body\s+\*|body\s+html|\*|html|body)(?=[,.{\[\s]|$)/is', '.foo', $data)."</pre>";
=========================> echo“.preg_replace('/(^ | | | | | | | | s]+)(正文html |正文html |正文html |正文)([^,.{\s]*)/is', “$1.”.foo.“$3.”,$data.”;
多亏了cbuckley,我不得不使用lookahead和lookahead,现在它工作得很好;) 我做了一些改进,所以它可以匹配现在的任何可能性

工作代码:

.foo { color: red; }
.foo.class { color: red; }
.foo,.foo { color: red; }
.foo[dir="rtl"], .foo, .body table, #html-container
{
    background-color:#eee;
}
修改的css:


同一选择器的多次出现可以在以后过滤掉。

不要使用正则表达式来处理HTML和CSS,而是使用HTML解析器。匹配项在替换中不能重叠,因此在第3行中它匹配
HTML,
(包括结束空间)然后无法匹配
body
。您能推荐一个适合我需要的html解析器吗?我已经使用csstidy来简化处理,但它不能简单地用另一个选择器替换。我也尝试了Sabberworm,但由于名称空间的原因,无法将其提交wrok:(@cbuckley啊..谢谢我知道了..向前看行吗?我试试..是的,知道了,使用正则表达式是:
”/(?
$data = "html body { color: red; }\nhtml *.class { color: red; }\nhtml,body { color: red; }\nhtml[dir=\"rtl\"], body, .body table, #html-container\n{\n    background-color:#eee;\n}";
echo "<pre>".preg_replace('/(?<=^|[,}\]\s])(\*\s+html\s+body|\*\s+body\s+html|html\s+\*\s+body|html\s+body\s+\*|body\s+\*\s+html|body\s+html\s+\*|\*\s+html|\*\s+body|html\s+\*|html\s+body|body\s+\*|body\s+html|\*|html|body)(?=[,.{\[\s]|$)/is', '.foo', $data)."</pre>";
html body { color: red; }
html *.class { color: red; }
html,body { color: red; }
html[dir="rtl"], body, .body table, #html-container
{
    background-color:#eee;
}
.foo { color: red; }
.foo.class { color: red; }
.foo,.foo { color: red; }
.foo[dir="rtl"], .foo, .body table, #html-container
{
    background-color:#eee;
}