假设我有一个包含以下内容的HTML页面:
<meta property="image" content="http://example.com/example.jpg" />
如何使用PHP到达该“元属性”并从“内容”获取图像URL?
谢谢!
实时工作:http://codepad.org/ggUwL06F
$doc = new DOMDocument();
$doc->loadHTML('<meta property="image" content="http://example.com/example.jpg" />');
$metas = $doc->getElementsByTagName('meta');
foreach($metas as $meta){
if($meta->getAttribute('property') == 'image') {
$meta_image = $meta->getAttribute('content');
break;
}
}
echo $meta_image;
不要使用正则表达式来解析HTML。就像@ Kiwi1指出的那样,您可以使用PHP内置函数get_meta_tags()
。尽管我更喜欢使用DOM来解析HTML,即使是简单的东西也是如此。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句