我的内容为:
<meta property="og:type" content="article" />
<meta property="og:url" content="http://website/fox/" />
<meta property="og:site_name" content="The Fox" />
<meta property="og:image" content="http://images.Fox.com/2014/09/foxandforset.gif?w=209" />
<meta property="og:title" content="Fox goes to forest" />
我的要求是提取/获取一行,即meta property=og:image..
,因此结果应包含:
<meta property="og:image" content="http://images.Fox.com/2014/09/foxandforset.gif?w=209" />
答案 0 :(得分:1)
答案 1 :(得分:1)
提取HTML的“行”或使用正则表达式来解析HTML一般都很脆弱。更强大的是使用HTML解析器,例如DOM extension提供的支持。
$html = <<<'HTML'
<meta property="og:type" content="article" />
<meta property="og:url" content="http://website/fox/" />
<meta property="og:site_name" content="The Fox" />
<meta property="og:image" content="http://images.Fox.com/2014/09/foxandforset.gif?w=209" />
<meta property="og:title" content="Fox goes to forest" />
HTML;
$dom = new DOMDocument();
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$nodes = $xpath->query('//meta[@property="og:image"]');
foreach ($nodes as $node) {
echo $dom->saveHTML($node);
}
<meta property="og:image" content="http://images.Fox.com/2014/09/foxandforset.gif?w=209">