Home >Backend Development >PHP Tutorial >请问我这个正则为什么匹配不到呢?

请问我这个正则为什么匹配不到呢?

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-23 14:06:471037browse

$newstext='<li style="margin: 0px 20px 0px 0px; padding: 0px; list-style-type: none; float: left; text-align: center; line-height: 20px; width: 120px; height: 114px; overflow: hidden; "><a class="gaga" href="http://www.clvyou.com/jingdiafasdfsadfadsadsn/10063.html" title="长江三峡豪华游轮维多利亚凯珍号" target="_blank" style="margin: 0px; padding: 0px; text-decoration: none; color: rgb(0, 0, 0); "><img src="http://www.clvyou.com/d/file/titlepic/jingdia10063.jpg" alt="长江三峡豪华游轮维多利亚凯珍号"   style="max-width:90%" /><br style="margin: 0px; padding: 0px; " />长江三峡豪华游轮维多利亚凯珍号</a></li>';//$newstext='fasdadsafsasdasf<a title="gga" href="http://www.baidu.com">百度</a>fadsfadsfadsf<a href=\'http://www.baidu.com\' target="_blank">百度</a>fadsfadsfads';可以匹配这样的,但是上面的就匹配不到preg_match_all('/<a.+href=["|\'](.*)["|\'].*>(.*)<\/a>/iU',$newstext,$result);print_r($result);exit;


回复讨论(解决方案)

找到问题了,a标签里有换行,要把.*改成[\s\S]*
正确如下:
preg_match_all('/([\s\S]*)/iU',$newstext,$result);

得到
Array
(
    [0] => Array
        (
            [0] => 请问我这个正则为什么匹配不到呢?
长江三峡豪华游轮维多利亚凯珍号

        )

    [1] => Array
        (
            [0] =>  http://www.clvyou.com/jingdiafasdfsadfadsadsn/10063.html
        )

    [2] => Array
        (
            [0] =>  请问我这个正则为什么匹配不到呢?
长江三峡豪华游轮维多利亚凯珍号
        )

)

不就是你想要的吗?

如果a链接里有换行不行的

/(.*)/i sU
就可以了

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn