如何循环遍历从XPath部分匹配创建的数组以显示XML元素

时间:2022-10-23 21:35:57

I have been working on this for a while now and I am at a complete block, I am not sure if I am going about it the right way.

我已经在这方面工作了一段时间,而且我已经完全阻止,我不确定我是否会以正确的方式进行。

What I have is an RSS feed displayed on my website. I have created a search box where I require this to partially match against title/description of the XML.

我所拥有的是我网站上显示的RSS源。我创建了一个搜索框,我要求它与XML的标题/描述部分匹配。

  <?php
    $xml = <<<XML
    XML;
    $sxe = simplexml_load_file('http://www.music-news.com/rss/news.asp');
    $searchKeyword = $_POST["SearchBox"];
    $area = $sxe->xpath("//item[contains(title,'".$searchKeyword."')]");
    print_r($area);
   ?> 

This code when called up from the submit button of the search box will bring to a page that just displays the follow

从搜索框的提交按钮调出时,此代码将显示一个只显示以下内容的页面

Array ( [0] => SimpleXMLElement Object ( [title] => SimpleXMLElement Object ( ) [description] => SimpleXMLElement Object ( ) [link] => SimpleXMLElement Object ( ) [pubDate] => Mon, 27 Apr 2015 10:01:00 GMT [guid] => SimpleXMLElement Object ( ) [author] => SimpleXMLElement Object ( ) ) )

It is bringing back the correct records within the RSS feed (can be determined from the publish date). However I am trying to get it so that the search results would show as the original RSS feed shows on the site. The code for the initial RSS feed is below

它会在RSS提要中恢复正确的记录(可以从发布日期确定)。但是我试图得到它,以便搜索结果显示为原始RSS提要在网站上显示。初始RSS提要的代码如下

<?php

$rss = simplexml_load_file('http://www.music-news.com/rss/news.asp');

foreach ($rss->channel->item as $item) 
{
echo "<h2>" .$item->title . "</h2>";
echo "<p>" . $item->pubDate . "</p>";
echo "<p>" . $item->description .  "</p>";
echo '<p><a href="'. $item->link .'">' .ReadMore. "</a><p>";
} 
?>

Any assistance would be greatly appreciated

任何帮助将不胜感激

Thanks in advance

提前致谢

1 个解决方案

#1


0  

$area should be an array of zero to many simplexml-elements, so use:

$ area应该是一个零到多个simplexml元素的数组,所以使用:

foreach($area as $item) {
    echo "<h2>" .$item->title . "</h2>";
    echo "<p>" . $item->pubDate . "</p>";
    // and so on
} 

see a working example: https://eval.in/319904

看一个有效的例子:https://eval.in/319904

BTW, you should sanitize $searchKeyword to prevent any kind of harmful input, see Cleaning/sanitizing xpath attributes, specifically the top rated answer has a final paragraph on sanitizing with PHP.

顺便说一句,您应该清理$ searchKeyword以防止任何类型的有害输入,请参阅清理/清理xpath属性,特别是最受好评的答案有一个关于使用PHP进行清理的最后一段。

#1


0  

$area should be an array of zero to many simplexml-elements, so use:

$ area应该是一个零到多个simplexml元素的数组,所以使用:

foreach($area as $item) {
    echo "<h2>" .$item->title . "</h2>";
    echo "<p>" . $item->pubDate . "</p>";
    // and so on
} 

see a working example: https://eval.in/319904

看一个有效的例子:https://eval.in/319904

BTW, you should sanitize $searchKeyword to prevent any kind of harmful input, see Cleaning/sanitizing xpath attributes, specifically the top rated answer has a final paragraph on sanitizing with PHP.

顺便说一句,您应该清理$ searchKeyword以防止任何类型的有害输入,请参阅清理/清理xpath属性,特别是最受好评的答案有一个关于使用PHP进行清理的最后一段。