“&"号导致返回的文本在URL中被截断 [英] Ampersand causing returned text to be cut off in URL

查看:113
本文介绍了“&"号导致返回的文本在URL中被截断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经从以前维护代码的人那里继承了几个php脚本.他没有编程经验,大部分时间都竭尽所能.今天,我不得不为wordpress重写一个插件文件,现在我无法弄清楚出了什么问题.

I have inherited several php scripts from the person who maintaned the code before. He had no programming experience and mostly threw things together the best he could. today I had to rewrite a plug-in file for wordpress and now I can't figure out what is going wrong.

我面临的问题是,当在表(<?php echo $result->display_name; ?>)的else语句中对返回的文件名进行显示时,我得到了意外的结果.假设我们的数据库有一个名为Black&白色.pdf.而不是返回显示名称"Black& White.pdf",而是返回"Black".我尝试了以下方法:

The issue I am facing is that when diplaying the returned file names in the else statement that is part of the table (<?php echo $result->display_name; ?>) I get unexpected results. Let's say our database has a file called Black & White.pdf. Instead of returning the display_name "Black & White.pdf", it returns "Black". I have tried the following:

<?php echo htmlentities($result->display_name); ?>

<?php echo htmlspecialchars($result->display_name); ?>

两者均未产生期望的结果.怎么了?这是我对PHP的无知,还是关于WP如何返回结果的原因(我认为这不会有所作为,因为我不相信WP会改变php的解析方式).供参考的代码是:

Neither of which have produced the desired result. What is going wrong? Is this PHP ignorance on my part, or is this something about how WP returns the results (which I assume wouldn't make a difference since I don't believe that WP could change how php is parsed). For reference here is the code:

function display_files($assn_id) {
    global $wpdb;
    $second_db = new wpdb("xxxxxx", "xxxxxx", "xxxxxx", "xxxxxx");

    $results = $second_db->get_results("SELECT
        community_files.id,
        community_files.display_name,
        community_files.filename,
        community_files.sort,
        community_files.file_type
        FROM
        community_files
        WHERE
        community_files.comm_id = '".$assn_id."'
        ORDER BY
        community_files.sort ASC");

        if (!$results) {
            echo "<li>The next meeting has not been posted yet.</li>";
        } else {

            echo "<table>";
            // keeps getting the next row until there are no more to get

            foreach ($results as $result) {
                if (!$result->display_name) {
                    $display_name = str_replace("_", " ", $result->filename);
                    $display_name_fake = str_replace(" .", ".", $display_name);
                    $file_array[$x] = $result->id;
                } else {
                    $display_name = $result->display_name;
                    $display_name_fake = str_replace(" .", ".", $display_name);
                    $file_array[$x] = $result->id;
                } ?>
                    <tr>
                        <td>
                        <?php
                        if ($result->display_name == "") {
                        ?>
                            <a href="renamefiles.php?action=rename&amp;file_id=<?php echo $result->id; ?>&amp;filename=<?php echo $display_name; ?>&amp;assn_id=<?php echo $assn_id; ?>"><?php echo $display_name_fake; ?></a>
                        <?php
                        } else {
                        ?>
                            <a href="renamefiles.php?action=rename&amp;file_id=<?php echo $result->id; ?>&amp;filename=<?php echo $display_name; ?>&amp;assn_id=<?php echo $assn_id; ?>"><?php echo $result->display_name; ?></a>
                        <?php
                        }
                        ?>
                        </td>
                    </tr>
                <?php   
            }
            echo "</table>";
        }
}

推荐答案

听起来您需要

It sounds like you need to urlencode that file name when you insert it into the URL you are constructing. Per a comment above not doing so is breaking your GET string.

array(5) { 
  ["action"]=> string(6) "rename" 
  ["file_id"]=> string(5) "24086" 
  ["filename"]=> string(8) "Black " 
  ["White_pdf"]=> string(0) "" 
  ["assn_id"]=> string(2) "25" 
}

这使您得出的结论是您发布的代码失败.不是.它只是构造格式错误的HTML,然后在您单击类似的内容时失败.

This has led you to conclude that the code you posted is failing. It isn't. It is just constructing malformed HTML, which then fails when you click the like.

此代码:

<a href="renamefiles.php?action=rename&amp;file_id=<?php echo $result->id; ?>&amp;filename=<?php echo $display_name; ?>&amp;assn_id=<?php echo $assn_id; ?>"><?php echo $result->display_name; ?></a>

应该是这样的:

<a href="renamefiles.php?action=rename&amp;file_id=<?php echo $result->id; ?>&amp;filename=<?php echo urlencode($display_name;) ?>&amp;assn_id=<?php echo $assn_id; ?>"><?php echo $result->display_name; ?></a>

处理时,您需要 urldecode $_GET['filename']请求.

You will then need to urldecode $_GET['filename'] when you process the request.

我鼓励您在文件名中根本不要包含空格和特殊字符(例如&)(如果您实际上使用的是此名称作为文件系统文件名).你在找麻烦.

I would encourage you not to have spaces and special characters like & in your filename at all (if you actually are using this name as a filesystem file name). You are asking for trouble.

这篇关于“&"号导致返回的文本在URL中被截断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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