为什么 preg_replace 会抛出“未知修饰符”错误?[复制]
-
06-07-2019 - |
题
这个问题在这里已经有答案了:
- 警告:预替换():未知修饰符“]” 3 个回答
我不断收到此错误:
警告:preg_match()[function.preg-match]:d: xampp htdocs tavisionator components com_smms functions functions plugin.php in Line 235中的未知修饰符't'
在:
$PageContent = preg_replace($result->module_pregmatch, '', $PageContent);
我对 $result->module_pregmatch 执行 var_dump,得到以下结果:
string '/<title>(.*)</title>/Ui' (length=23)
string '/<meta[^>]*name=["|\']description["|\'][^>]*content=["|\'](.*)["|\']\s*\/>/Ui' (length=77)
string '/<meta[^>]*name=["|\']keywords["|\'][^>]*content=["|\'](.*)["|\']\s*\/>/Ui' (length=74)
string '/<meta[^>]*name=["|\']author["|\'][^>]*content=["|\'](.*)["|\']\s*\/>/Ui' (length=72)
string '/<meta[^>]*name=["|\']copyright["|\'][^>]*content=["|\'](.*)["|\']\s*\/>/Ui' (length=75)
string '/<meta[^>]*name=["|\']robots["|\'][^>]*content=["|\'](.*)["|\']\s*\/>/Ui' (length=72)
string '/<meta[^>]*http=equiv=["|\']content-language["|\'][^>]*content=["|\'](.*)["|\']\s*\/>/Ui' (length=88)
string '/<meta[^>]*http-equiv=["|\']content-type["|\'][^>]*content=["|\'](.*)["|\']\s*\/>/Ui' (length=84)
string '/<link[^>]*href=["|\'](.*)["|\'][^>]*rel=["|\']shortcut[^>]*icon["|\'][^>]*type=["|\']image\/x-icon["|\']\s*\/>/Ui' (length=114)
string '/<link[^>]*href=["|\'](.*)["|\'][^>]*rel=["|\']alternate["|\'][^>]*type=["|\']application\/rss\+xml["|\'][^>]*title=["|\'](.*)["|\'][^>]\/>/Ui' (length=142)
string '/<link[^>]*href=["|\'](.*)["|\'][^>]*rel=["|\']alternate["|\'][^>]*type=["|\']application\/atom\+xml["|\'][^>]*title=["|\'](.*)["|\'][^>]\/>/Ui' (length=143)
有人可以告诉我我做错了什么吗?我已经被这个错误困扰太久了......
解决方案
您使用正斜杠作为正则表达式模式分隔符,所以 /<title>(.*)</title>/Ui'
不管用 (</title>
有一个正斜杠)。
您应该能够转义正斜杠或使用模式中未包含的不同分隔符,例如
'/<title>(.*)<\/title>/Ui' //(esacaping)
或者
'~<title>(.*)</title>~Ui' //different delimiter
不隶属于 StackOverflow