0

如何从视图源中提取 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>

谢谢

4

1 回答 1

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
        )

)
于 2012-05-05T14:09:28.190 回答