简体   繁体   English

预浸替换错误

[英]preg-replace error

Can anyone find the solution for this error, 谁能找到解决此错误的方法,

     $str=  trim(strip_tags($str));

$str = preg_replace("'<style[^>]*>.*</style>'siU",'',$str);

    $patterns = array();
$patterns[0] = '&nbsp; ';
$patterns[1] = ' &nbsp;';
$patterns[2] = '&nbsp;';
$replacements = array();
$replacements[0] = '';
$replacements[1] = '';
$replacements[2] = ' ';

$str    =   preg_replace($patterns, $replacements, $str);

It showing this error. 它显示此错误。

Message: preg_replace() [function.preg-replace]: No ending delimiter '&' found 消息:preg_replace()[function.preg-replace]:找不到结尾定界符“&”

-Arun -阿伦

You have the anwser in the error code, regex needs delimiters to work. 您在错误代码中包含了anwser,正则表达式需要定界符才能起作用。

And you should use str_replace for &nbsp; 并且您应该将str_replace用于&nbsp; .

The first character in your pattern is interpreted as a delimiter and right now PHP thinks you want to use & as a delimiter. 模式中的第一个字符将被解释为定界符,现在PHP认为您想使用&作为定界符。 In your first regex, the delimiter was ' , BTW. 在您的第一个正则表达式中,分隔符为' BTW。 I guess what you really want is not '&nbsp;' 我想您真正想要的不是'&nbsp;' , but '/&nbsp;/' (eg for pattern 2), now / is the delimiter (it is not part of the regex itself). ,但使用'/&nbsp;/' (例如对于模式2),现在/是分隔符(它不是正则表达式本身的一部分)。 BTW, your first regex seems not correct either, it should probably be 顺便说一句,您的第一个正则表达式似乎也不正确,应该是

'<style[^>]*>[^<]*</style>'

Otherwise the regex could match the first tag and the very last tag of the whole document. 否则,正则表达式可以匹配整个文档的第一个标签和最后一个标签。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM