问题:
Warning: preg_match(): Unknown modifier ‘/‘ in D:\wwwroot\www.xb.com\t.php on line 6
bool(false) NULL
我的代码:
<?php
$str = ‘‘;
$isMatched = preg_match("/http://www.chinanews.com/gn/2018/07-04/8555660.shtml/", $str, $matches);
var_dump($isMatched, $matches);
问题原因:
上述正则的定界符是斜杠’/’, 所以以此往后找,再找一个斜杠’/’,就结束了,得到的是:
/http:/
所以,后面跟着的内容:
/www.chinanews.com/gn/2018/07-04/8555660.shtml/
就没法识别了。
解决办法是:
1. 把定界符,改为别的,和当前正则表达式中的字符,不会冲突的,比如:
"#/http://www.chinanews.com/gn/2018/07-04/8555660.shtml#";
就可以了。
2. 在定界符内的‘/‘前面加上转义字符‘\‘, 如下
"/http:\/\/www.chinanews.com\/gn\/2018\/07-04\/8555660.shtml/"
<?php
$str = ‘‘;
//方法一:
$isMatched = preg_match("#http://www.chinanews.com/gn/2018/07-04/8555660.shtml#", $str, $matches);
//方法二:
// $isMatched = preg_match("/http:\/\/www.chinanews.com\/gn\/2018\/07-04\/8555660.shtml/", $str, $matches);
var_dump($isMatched, $matches);
原文:https://www.cnblogs.com/zhidongjian/p/9264516.html