(PHP5) Extracting a title tag and RSS feed address from HTML using PHP DOM or Regex

蓝咒 提交于 2019-11-28 13:56:36

One approach

$dom = new DOMDocument;            // init new DOMDocument
$dom->loadHTML($html);             // load HTML into it
$xpath = new DOMXPath($dom);       // create a new XPath

$nodes = $xpath->query('//title'); // Find all title elements in document
foreach($nodes as $node) {         // Iterate over found elements
    echo $node->nodeValue;         // output title text
}

To get the href attribute of all link tags with a type of "application/rss+xml" you would use this XPath:

$xpath->query('//link[@type="application/rss+xml"]/@href');

RegExp is far away from the best solution ;) Use a feed reader, the Zend_Feed class of the zend framework for example.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!