我想从给定的URL获取标题标记和RSS提要地址(如果有的话),但到目前为止我使用的方法根本不起作用.我已经设法通过使用preg_match和正则表达式来获取标题标签,但我似乎无法获得RSS源地址.
($ webContent保存网站的HTML)
我已将我的代码复制到下面以供参考......
`//获取标题标签preg_match('@(.*)@ i',$ webContent,$ titleTagArray);
// If the title tag has been found, assign it to a variable
if($titleTagArray && $titleTagArray[3])
$webTitle = $titleTagArray[3];
// Get the RSS or Atom feed address
preg_match('@<link(.*)rel="alternate"(.*)href="(.*)"(.*)type="application/rss+xml"\s/>@i',$webContent,$feedAddrArray);
// If the feed address has been found, assign it to a variable
if($feedAddrArray && $feedAddrArray[2])
$webFeedAddr = $feedAddrArray[2];`
Run Code Online (Sandbox Code Playgroud)
我一直在读这里使用正则表达式不是最好的方法吗?希望有人可以帮我一把:-)
谢谢.
一种方法
$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
}
Run Code Online (Sandbox Code Playgroud)
要使用"application/rss + xml"类型获取所有链接标记的href属性,您将使用此XPath:
$xpath->query('//link[@type="application/rss+xml"]/@href');
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1452 次 |
| 最近记录: |