天天看点

正则匹配超链接

匹配超链接:

$pattern = "/<[a|A].*?href=[\'|\"]([^\'|^\"].*?)[\'|\"].*?>([^<]*?)<\/[a|A]>/";      

上面的匹配方式匹配不到<strong>标签  会导致<strong>点我进宝阁</strong>匹配结果为空

链接为:

$content = <a href="http://live.cf8.com.cn/product/show/3216" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank"><strong>点我进宝阁</strong></a>

匹配规则为:

$pattern = "/<[a|A].*?href=[\'|\"]([^\'|^\"].*?)[\'|\"].*?>([\s|\S]*?)<\/[a|A]>/";      
preg_match_all($pattern, $content, $match);      

print_r($match);

结果为:

Array
              (
              [0] => Array
              (
              [0] => <a href="http://live.cf8.com.cn/product/show/3216" target="_blank" rel="external nofollow"  target="_blank" rel="external nofollow"  title="点我进宝阁" target="_blank">              <strong>点我进宝阁</strong></a>
              )
                  [1] => Array
              (
              [0] => http://live.cf8.com.cn/product/show/3216
              )
                  [2] => Array
              (
              [0] => <strong>点我进宝阁</strong>
              )
                  )
           

这样就可以链接以及链接中的内容全部匹配到