使用PHP simple_html_dom.php提取META

时间:2014-01-10 14:59:36

标签: php html

我已阅读并尝试了每一篇关于此的POST但无法使其工作。 这是HTML:

<meta itemprop="interactionCount" content="UserPlays:4635">
<meta itemprop="interactionCount" content="UserLikes:4">
<meta itemprop="interactionCount" content="UserComments:0">

我需要提取'4635'位。

代码:

<?php
  $html = file_get_html($url);
foreach($html->find("meta[name=interactionCount]")->getAttribute('content') as $element) {
    $val = $element->innertext; 

    echo '<br>Value is: '.$val; 
}

我一无所获?

2 个答案:

答案 0 :(得分:0)

$metaData= '<meta itemprop="interactionCount" content="UserPlays:4635">
            <meta itemprop="interactionCount" content="UserLikes:4">
            <meta itemprop="interactionCount" content="UserComments:0">';

$dom = new DOMDocument();
$dom->loadHtml($metaData);
$metas = $dom->getElementsByTagName('meta');

foreach($metas as $el) {
  list($user_param,$value) = explode(':',$el->getAttribute('content'));
  // here check what you need
  print $user_param.' '.$value.'<br/>';
}

// OUTPUT 

UserPlays 4635

UserLikes 4

UserComments 0

答案 1 :(得分:0)

include 'simple_html_dom.php';
$url = '...';
$html = file_get_html($url);
foreach ($html->find('meta[itemprop="interactionCount"]') as $element) {
    list($key, $value) = explode(':', strval($key->content));
    echo 'Value:'.$value."\n";
}