【问题标题】:transform XML nodeValue to PHP/HTML string将 XML nodeValue 转换为 PHP/HTML 字符串
【发布时间】:2016-12-08 10:01:40
【问题描述】:

我正在使用 AJAX 实时搜索来生成特定于用户个人资料的链接。它运作良好,我总是以我想要的配置文件结束,但有一个问题。

让我们为用户 1 执行此操作(用户名 = testuser;user_id = 1;博客名 = testblog)。如果我搜索“test”,将显示两个链接,即指向 testuser 个人资料的链接和指向 testuser 博客的链接。现在奇怪的是,这些链接看起来像这样:

profile.php?user=1&page=profile

profile.php?user=1&page=blog

但实际链接如下所示:

profile.php?user=%20+%201%20+%20&page=profile

profile.php?user=%20+%201%20+%20&page=blog

由于我最终到达了我想要的页面,你可以说这并不重要,但它确实如此,因为我需要 $GET_['user'] 值始终是实数,而不是那种东西我正在处理,在这里。

我希望有一些简单的方法可以解决这个问题。像 nodeValue->string 什么的。我需要在这部分代码中更改 nodeValue 我认为:$z->item(0)->childNodes->item(0)->nodeValue

这是我正在使用的代码:

<?php
$xmlDoc=new DOMDocument();
$xmlDoc->load("../xml/accounts.xml");

$x=$xmlDoc->getElementsByTagName('account');

//get the q parameter from URL
$q=$_GET["q"];

//lookup all links from the xml file if length of q>0
if (strlen($q)>0) {

    $hint="";

    for($i=0; $i<($x->length); $i++) {
        $y=$x->item($i)->getElementsByTagName('username');
        $b=$x->item($i)->getElementsByTagName('blogname');
        $c=$x->item($i)->getElementsByTagName('companyname');
        $z=$x->item($i)->getElementsByTagName('user_id');



        //search for usernames
        if ($y->item(0)->nodeType==1) {

            //find a link matching the search text
            if (stristr($y->item(0)->childNodes->item(0)->nodeValue,$q)) {

                if ($hint=="") {
                    $hint=  "<a href='profile.php?user= + " . 
                        $z->item(0)->childNodes->item(0)->nodeValue .
                        " + &page=profile' >" .
                        $y->item(0)->childNodes->item(0)->nodeValue . "</a><span> (profile)</span>";

                } else {
                    $hint=  $hint . "<br /><a href='profile.php?user= + " .
                        $z->item(0)->childNodes->item(0)->nodeValue .
                        " + &page=profile' >" .
                        $y->item(0)->childNodes->item(0)->nodeValue . "</a><span> (profile)</span>";
                }
            }
        }




    //search for blognames
        if ($b->item(0)->nodeType==1) {

            //find a link matching the search text
            if (stristr($b->item(0)->childNodes->item(0)->nodeValue,$q)) {

                if ($hint=="") {
                    $hint=  "<a href='profile.php?user= + " . 
                        $z->item(0)->childNodes->item(0)->nodeValue .
                        " + &page=blog' >" .
                        $b->item(0)->childNodes->item(0)->nodeValue . "</a><span> (blog)</span>";

                } else {
                    $hint=  $hint . "<br /><a href='profile.php?user= + " .
                        $z->item(0)->childNodes->item(0)->nodeValue .
                        " + &page=blog' >" .
                        $b->item(0)->childNodes->item(0)->nodeValue . "</a><span> (blog)</span>";
                }
            }
        }


// Set output to "no suggestion" if no hint was found
// or to the correct values
if ($hint=="") {
    $response="no QuickResults, hit enter";
} else {
    $response=$hint;
}


//output the response
echo $response;
?>

在我的 XMLfile 中,如果有帮助,结构如下所示:

<account>
    <username>testuser</username>
    <user_id>1</user_id>
    <blogname>testblog</blogname>
</account>

【问题讨论】:

    标签: php xml string transform nodevalue


    【解决方案1】:

    您遇到的问题源于您的代码在结果链接中添加了空格和加号。并且空格会自动编码为%20。解决方案是将它们从代码中删除,如下所示:

    $hint=  "<a href='profile.php?user=" . 
             $z->item(0)->childNodes->item(0)->nodeValue .
             "&page=profile' >" .
             $y->item(0)->childNodes->item(0)->nodeValue . "</a><span> (profile)</span>";
    

    需要在所有四种情况下都进行此更改。

    【讨论】:

    • 完成了,它可以工作了,非常感谢你的帮助并告诉我:)
    • 乐于助人。如果您能将我的帖子标记为accepted answer,我会很高兴。谢谢!
    • 当然,我什至不知道;)
    猜你喜欢
    • 1970-01-01
    • 2015-09-25
    • 1970-01-01
    • 2012-08-23
    • 2013-08-14
    • 2018-08-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多