问题描述:

I have read and tried every POST about this but cannot get it to work.

This is the HTML:

<meta itemprop="interactionCount" content="UserPlays:4635">

<meta itemprop="interactionCount" content="UserLikes:4">

<meta itemprop="interactionCount" content="UserComments:0">

I need to extract the '4635' bit.

Code:

<?php

$html = file_get_html($url);

foreach($html->find("meta[name=interactionCount]")->getAttribute('content') as $element) {

$val = $element->innertext;

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

}

I get nothing back?

网友答案:
$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
网友答案:
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";
}
相关阅读:
Top