【问题标题】:PHP "&" character in $_GET variable issue$_GET 变量问题中的 PHP“&”字符
【发布时间】:2014-11-03 19:03:35
【问题描述】:

如何将“&”符号添加到 URL GET 变量中,使其成为字符串的一部分? 问题是它总是将字符串拆分到下一个变量。

我怎样才能做到这一点?

localhost/test.php?variable='jeans&shirts'    // so it executes it like a string

<?php

require "connect.php";

$variable = $_GET['variable'];

echo $variable;

?>

输出是'牛仔裤'

而不是“牛仔裤和衬衫”

【问题讨论】:

    标签: php url get


    【解决方案1】:

    你会想要urlencode()你的字符串:

    // Your link would look like this:
    'localhost/test.php?variable='.urlencode('jeans&shirts');
    

    当你想使用它时,你会解码它:

    echo $variable = urldecode($_GET['variable']);
    

    编码: http://php.net/manual/en/function.urlencode.php

    解码: http://php.net/manual/en/function.urldecode.php


    编辑:测试写这个:

    echo $url = 'localhost/test.php?variable='.urlencode('jeans&shirts');
    echo '<br />';
    echo urldecode($url);
    

    你的结果是:

    // Encoded
    localhost/test.php?variable=jeans%26shirts
    // Decoded
    localhost/test.php?variable=jeans&shirts
    

    【讨论】:

    • 这是误导。在 url 中使用它之前,您需要对字符串进行 urlencode。您的第一行代码根本不应该引用$_GET['variable'],而应该显示链接标签或类似标签
    • @Steve 抱歉,我正在更新你的评论,因为我知道这有点误导
    • 对我不起作用:\ 但没关系我通过排除 URL 中的“&”符号来解决问题
    • 在将变量转换为查询字符串时,您应该始终使用urlencode()。应该将$_GET['variable'] 输出到jeans%26shirts
    • 没问题,希望你能让它工作!它会在未来为您省去头疼的问题!
    猜你喜欢
    • 1970-01-01
    • 2010-09-10
    • 1970-01-01
    • 2013-05-15
    • 1970-01-01
    • 2019-11-30
    • 1970-01-01
    • 2019-02-10
    • 1970-01-01
    相关资源
    最近更新 更多