使用php获取所有<meta property =“”/>

时间:2012-05-05 13:54:27

标签: php

如何从视图源

中提取a的“内容”值

我使用CURL来获取页面的来源但在此之后我无法识别以下内容:

<meta property="og:title" content="Zoe&#39;s big surprise">
<meta property="og:description" content=" ">
<meta property="og:type" content="video">
<meta property="og:image" content="http://i1.ytimg.com/vi/PNyMlswZb-I/hqdefault.jpg">

基本上我需要捕获数组中的所有上述参数并使用它们。 我在curl之后尝试了以下内容

    //parsing begins here:
    $doc = new DOMDocument();
    @$doc->loadHTML($data);
    $nodes = $doc->getElementsByTagName('title');

    //get and display what you need:
    $title = $nodes->item(0)->nodeValue;

    $metas = $doc->getElementsByTagName('meta'); --This line is not able to fetch any <meta> content>

由于

1 个答案:

答案 0 :(得分:3)

这是我的解决方案:

$html = '<meta property="og:title" content="Zoe&#39;s big surprise">
<meta property="og:description" content=" ">
<meta property="og:type" content="video">
<meta property="og:image" content="http://i1.ytimg.com/vi/PNyMlswZb-I/hqdefault.jpg">';

$doc = new DOMDocument();
$doc->loadHTML($html);

foreach( $doc->getElementsByTagName('meta') as $meta ) { 
   $metaData[] = array(
        'property' => $meta->getAttribute('property'),
        'content' => $meta->getAttribute('content')
    );
}

print_r( $metaData );

输出:

Array
(
    [0] => Array
        (
            [property] => og:title
            [content] => Zoe's big surprise
        )

    [1] => Array
        (
            [property] => og:description
            [content] =>  
        )

    [2] => Array
        (
            [property] => og:type
            [content] => video
        )

    [3] => Array
        (
            [property] => og:image
            [content] => http://i1.ytimg.com/vi/PNyMlswZb-I/hqdefault.jpg
        )

)