0

我正在尝试使用 PHP 从 XML 文件中检索单个部分。这是我正在使用的代码:

<?php
$articles = simplexml_load_file('articles.xml');

foreach ($articles as $articlecontent)
{
    $title   = $articlecontent->title;
    $content = $articlecontent->content;
    $author  = $articlecontent->author;
    $date    = $articlecontent['date'];

    echo "<h1>",$title,"</h1>\n",
         "<p><i>",$date,"</i></p>\n",
         "<p>",$content,"</p>\n",
         "<p>",$author,"</p>\n",
         "<p><time>",$date,"</time></p>\n"
    ;
}
?>

其中显示了 XML 文件中的所有部分,但是我如何只解析一个结果,但使用查询字符串来解析呢?

示例:articles.php?title=section-one将检索标题为“section-one”的 XML 部分

4

4 回答 4

0

foreach如果标题过滤器正在运行并且不匹配,您可以通过在循环测试中插入一个条件来实现这一点。如果是这样,请使用continue关键字跳过该条目:

$titleFilter = trim(isset($_GET['title']) ? $_GET['title'] : '');

...

foreach ($articles as $articlecontent)
{
    $title   = $articlecontent->title;

    if (strlen($titleFilter) and $title !== $titleFilter) {
        continue;
    }

    ...
于 2013-05-02T14:10:17.777 回答
0

从 url 获取标题的方法是使用 $_GET 数组,然后按标题获取所需的文章,我认为您可以使用不需要 foreach 循环的 xpath

$title = $_GET['title']; 
// there should go some sanitizing
$articles = simplexml_load_file('articles.xml');

//search for article by title
//assuming that xml root tag is articles and article tag is article and searching by attribute named title
$foundArticles = $articles->xpath('articles/article[@title='.$title.']'); 
//query depends on xml structure 
// now you have array of matched articles with matching title from url
foreach($foundArticles as $singleArticle)
{
    //do printing here
}

此代码未经测试,但原则应该有效。

于 2013-04-29T18:32:58.113 回答
0

您可以从 PHP 的超全局数组中获取查询字符串的标题$_GET,如下所示:

$title = false;
if (isset($_GET['title']))
{
    $title = $_GET['title'];
}

然后最简单的方法是将其与foreach循环中 XML 文件的标题进行比较。

于 2013-04-29T18:16:22.017 回答
0

要从 URL 接收查询字符串变量(在我们的例子中为“标题”),您也可以使用$_REQUEST。一点点更正的代码 (@user1597483) 以及一些更高级的更新。

$articles = simplexml_load_file('articles.xml');
$title = "";
if(isset($_REQUEST['title'])) //receiving Query String from URL
    $title = $_REQUEST['title'];
if(count($articles)):
    //Fixed search  means that will one return article if 'section-one' in title
    //$result = $articles->xpath("//article[title='Section-one']");

    //Match All Search means that will return all articles that contains 'section-one' in title
    $result = $articles->xpath("//article[contains(translate(title, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'),'".strtolower($title)."')]");
    foreach ($result as $articleItem):
        $title=$articleItem->title;
        $content=$articleItem->content;
        $author=$articleItem->author;
        $date=$articleItem->date;
        echo "<h1>",$title,"</h1>\n";
        echo "<p><i>",$date,"</i></p>\n";
        echo "<p>",$content,"</p>\n";
        echo "<p>",$author,"</p>\n";
        echo "<p><time>",$date,"</time></p>\n";
    endforeach;
endif;

在上面的代码中,我指定了两种类型的过滤器,通过它们您可以通过精确匹配或通配符类型匹配contains功能获得特定文章的结果。而且我还使用translate了将title元素的值转换为小写并将查询字符串变量的值转换为小写的函数。因此,无论大小写如何,条件都将使用小写进行检查,如果条件匹配,则将返回结果文章。

于 2013-04-30T05:52:15.507 回答