使用克拉 (^) 时 preg_match_all 不匹配 [英] preg_match_all doesn't match when using a carat (^)
本文介绍了使用克拉 (^) 时 preg_match_all 不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用 preg_match_all
在 HTML 文件中查找 URL.URL 总是出现在行首,没有前导空格,如下所示:
I'm using preg_match_all
to find a URL in a HTML file. The URL always appears at the start of the line, with no leading space, like this:
<A HREF="/link/to/here"><strong>Next</strong></A>
我用它来匹配它:
preg_match_all('|^<A HREF="(?<url>.*?)"><strong>Next</strong>|', $html, $url_matches);
直到我删除克拉 (^) 字符后,它才起作用.我认为克拉与线的开头相匹配.为什么会导致我的匹配失败?
It didn't work until I removed the carat (^) character. I thought that the carat matched the start of a line. Why is it causing my match to fail?
推荐答案
你必须添加 m
修饰符:
You have to add the m
modifier:
preg_match_all('|^<A HREF="(?<url>.*?)"><strong>Next</strong>|m', $html, $url_matches);
then ^
在一行的开头匹配,否则它只会在整个字符串的开头匹配.
then ^
matches at start of a line, else it would only match at the start of the entire string.
更多信息:http://php.net/manual/en/reference.pcre.pattern.modifiers.php
这篇关于使用克拉 (^) 时 preg_match_all 不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文