drf21989 2016-12-08 10:01
浏览 82
已采纳

将XML nodeValue转换为PHP / HTML字符串

I am using AJAX live search to generate user-profile-specific links. It works well, I always end up at the profile I want to, but there ist an issue.

Let's do this for user 1 (username = testuser; user_id = 1; blogname = testblog). If I search for "test", both links will be displayed, the link to testuser's profile, and the link to testuser's blog. The strange thing now is, the links work as if they would look like this:

profile.php?user=1&page=profile

profile.php?user=1&page=blog

but the actual links look like this:

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

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

Since I end up on the page I want to, you could say it doesn't matter, but it does, because I need the $GET_['user'] values always to be real numbers, not that kind of stuff I'm dealing with, here.

I hope there is some easy way to fix this. Like nodeValue->string or something. I need to change the nodeValue in this part of the code I think: $z->item(0)->childNodes->item(0)->nodeValue

This is the code I'm using:

<?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;
?>

Inside my XMLfile the structure looks like this, if it helps:

<account>
    <username>testuser</username>
    <user_id>1</user_id>
    <blogname>testblog</blogname>
</account>
  • 写回答

1条回答 默认 最新

  • douchun9719 2016-12-08 11:34
    关注

    The problem you are getting arises from the fact that your code adds spaces and a plus sign to the resulting link. And spaces are automatically encoded as %20. The solution would be to remove them from the code like this:

    $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>";
    

    This change would need to be done in all four occurences.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘