PHP Regex HTML-提取URL [英] PHP Regex HTML - Extract URL

查看:65
本文介绍了PHP Regex HTML-提取URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用正则表达式从HTML文件中提取多个URL. 文件中还有其他URL,我唯一拥有的模式是"tableentries".和"

I am trying to extract multiple URLs from HTML file with regex. There are other URLs in the file, do the only pattern i have is "tableentries." and ""

HTML代码示例:

<tr class="tableentries2">
  <td>
    <a href="http://example.com/all-files/files/00000000789/">Click Here</a>
  </td>

我写的PHP:

$html = "value of the code above"
if(preg_match_all('/<td>.*</td>/', $html, $match)){
foreach($match[0] as $x){

echo $x . "<br>";

}}

推荐答案

为什么不仅仅查找href值? (已更新,因为已编辑的代码现在带有引号.)

Why not just look for href values? (Updated because the edited code now has quotation marks.)

preg_match_all('/href="([^\s"]+)/', $html, $match);

然后URI将在$match[1][0]中.

这篇关于PHP Regex HTML-提取URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆