我正在尝试运行我在在线php regex测试网站上调试的正则表达式。
这是完整的正则表达式:
$rule = '/[\s\S]*<(b|strong)>[\s\S]*notes:[\s\S]*<(\/b|\/strong)>([\s\S]*?)<(b|strong)>(sources|source|citation|data sources|last update)[\s\S]*/im';
即使使用更简单的测试正则表达式:
$rule = "/[\s\S]*<(b|strong)>[\s\S]*/im";
我仍然从此代码中收到错误:
$matches = array();
preg_match($source_html, $rule, $matches);
我收到的错误是:
警告:preg_match():未知的修饰符&#39;&amp; lt;&#39;在... / FragmentGrabber.php第63行
我在我的智慧结束时尝试在我的本地方框上解决此问题。无论如何都有解决此警告并使我的正则表达式无误地工作。我试过修复我的正则表达式的转义字符,但这没有帮助。也许有一个字符编码问题?!请注意输出中的文字<
与<
。
修改
以下是我班级的片段,如果有人真的想要,我会发布整件事。
// d() below is the Kint debugger.
public function get_fragments_by_name($source_html, $name)
{
$matches = array();
$rule = self::generate_regex_rule($name);
preg_match($source_html, $rule, $matches);
return $matches;
}
private function generate_regex_rule($name)
{
$end_tags = self::get_end_tag_options($name);
$rule = "/[\s\S]*<(b|strong)>[\s\S]*/";# . $name . ":[\s\S]*<(\/b|\/strong)>([\s\S]*?)<(b|strong)>(" . $end_tags . ")[\s\S]*/im";
d($rule);
return $rule;
}
答案 0 :(得分:4)
preg_match
个参数的顺序错误。首先应该是正则表达式。
所以改变:
preg_match($source_html, $rule, $matches);
为:
preg_match($rule, $source_html, $matches);
答案 1 :(得分:2)
我认为你应该尝试将$ rule作为第一个参数传递:
preg_match($rule, $source_html, $matches);
答案 2 :(得分:1)
必须像这样使用参数(来自PHP文档):
int preg_match(string $ pattern,string $ subject [,array&amp; $ matches [,int $ flags = 0 [,int $ offset = 0]]])
所以你需要这样做:
preg_match($rule, $source_html, $matches);