天天看點

正則比對超連結

比對超連結:

$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>
              )
                  )
           

這樣就可以連結以及連結中的内容全部比對到