将 XML nodeValue 转换为 PHP/HTML 字符串 [英] transform XML nodeValue to PHP/HTML string

查看:23
本文介绍了将 XML nodeValue 转换为 PHP/HTML 字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

但实际链接如下所示:

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

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

既然我最终出现在我想要的页面上,您可以说这无关紧要,但确实如此,因为我需要 $GET_['user'] 值始终是实数,而不是那种东西我正在处理,在这里.

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.

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

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

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

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

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

推荐答案

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

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.

这篇关于将 XML nodeValue 转换为 PHP/HTML 字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆