preg_match_all doesn't match when using a carat (^)

后端 未结 2 1343
礼貌的吻别
礼貌的吻别 2021-01-22 00:52

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:



        
相关标签:
2条回答
  • 2021-01-22 01:34

    ^ matches start-of-string not start-of-line. Use the m ("multi-line") modifier: //m

    0 讨论(0)
  • 2021-01-22 01:39

    You have to add the m modifier:

    preg_match_all('|^<A HREF="(?<url>.*?)"><strong>Next</strong>|m', $html, $url_matches);
    

    then ^ matches at start of a line, else it would only match at the start of the entire string.

    More Info: http://php.net/manual/en/reference.pcre.pattern.modifiers.php

    0 讨论(0)
提交回复
热议问题