使用PHP preg_match_all,获得href的价值 [英] With PHP preg_match_all, get value of href

查看:114
本文介绍了使用PHP preg_match_all,获得href的价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即使我阅读了本教程,我也并没有真正理解过正则表达式的工作原理 http://www.webcheatsheet.com/php/regular_expressions.php

I don'T really understabd how regular expressions works even after I read this tutorial http://www.webcheatsheet.com/php/regular_expressions.php

这是我需要找到的:

<link type="text/html" rel="alternate" href="http://link"/>

它应该返回:

http://link

这是我尝试过的:

$find = preg_match_all(
    '/<link type="text/html" rel="alternate" href=".*',
    $file,
    $patterns2
);

你可以笑了:)

在此先感谢您的帮助和时间:)

Thanks in advance for your help and your time :)

推荐答案

使用simplexml

using simplexml

$html = '<link type="text/html" rel="alternate" href="http://link"/>';
$xml  = simplexml_load_string($html);
$attr = $xml->attributes();

使用dom

$dom = new DOMDocument;
$dom->loadHTML($html);
$nodes = $dom->getElementsByTagName('link');
$attr  = $nodes->item(0)->getAttribute('href');

这篇关于使用PHP preg_match_all,获得href的价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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