doushan1157 2015-01-21 06:37
浏览 83
已采纳

使用DOMDocument在PHP中刮取特定标记属性

I am trying to extract the content from 'meta' tag depending upon the 'property'. Like `

<meta name="keywords" content="9gag,fun,funny,lol,meme,GIF,wtf,omg,fail,video,cosplay,geeky,forever alone" />
<meta name="twitter:image" content="http://images-cdn.9gag.com/images/thumbnail-facebook/14198244_1420182794.8999_AmeJun_n.jpg" />
<meta property="og:title" content="I finished the manga last week, so I wanted to make my on &quot;What Naruto taught me&quot;" />
<meta property="og:site_name" content="9GAG" />
<meta property="og:url" content="http://9gag.com/gag/aGVqbvz" />

... ` so I want to get only those content having 'og'. Through a cURL request I have been able to get attributes.

$ch = curl("http://9gag.com/gag/aGVqbvz?ref=fsidebar");
$dom = new DOMDocument();
@$dom->loadHTML($ch);

//echo $ch;
$links = $dom->getElementsByTagName('meta');
//get no of tags or elements
echo $links->length;
echo '<pre>';
foreach ($links as $link) {
    echo $link->getAttribute("property");
    echo '<br>';
}

how can I get content specific to only particular property or name.

  • 写回答

1条回答 默认 最新

  • duanlao1552 2015-01-21 06:44
    关注

    XPath is your friend here. An expression like //meta[starts-with(@property, "og")]/@content will grab the content attributes for all meta elements that have a property attribute whose value begins with "og".

    Example:

    $xpath = new DOMXPath($dom);
    $query = '//meta[starts-with(@property, "og")]/@content';
    foreach ($xpath->query($query) as $node) {
        echo $node->value, "
    ";
    }
    

    Output:

    I finished the manga last week, so I wanted to make my on "What Naruto taught me"
    9GAG
    http://9gag.com/gag/aGVqbvz
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?