php获取页面所有链接的正则表达式

$pattern = '/<a(?:.*?)href="(((?:http(?:s?):\/\/)?([^\"\/]+))?(?:[^\"]*))"(?:[^>]*?)>([^<]*?)<\/a>/i';
preg_match_all($pattern, $data, $links);
$links[0]是所有<a>标签。
$links[1]是所有href。
$links[2]是所有带http(s)://链接。
$links[3]是所有域名,如:www.cnblogs.com。
$links[4]是所有a标签内容。
直接获取<a>标签:$pattern = '/<a.*?<\/a>/i';
仅获取href数据:$pattern = '/<a(?:.*?)href=[\'"]([^\"\']*)[\'"][^<]*?<\/a>/i';        //仅href

单双引号,href前换行,href后等号前后夹杂空格,a标签内嵌标签等一堆情况解决方案:
$pattern = '/<a(?:[\s\S]*?)href\s*?=\s*?[\'"](((?:http(?:s?):\/\/)?([^\"\'\/]+))?(?:[^\"\']*))[\'"](?:[^>]*?)>([\s\S]*?)<\/a>/i';

演示网址:phpParseLinks

修改:上面pattern中新增?,红色标记。修复无http://与域名情况。

相关文章:

  • 2022-12-23
  • 2022-02-17
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-08-14
  • 2021-06-18
  • 2022-12-23
  • 2021-07-21
  • 2021-09-28
  • 2022-12-23
  • 2021-08-19
相关资源
相似解决方案