PHP“&&引用;美元中的字符\u获取变量问题
如何将“&”符号放入URL GET变量,使其成为字符串的一部分? 问题是它总是将字符串拆分为下一个变量 我怎样才能做到这一点PHP“&&引用;美元中的字符\u获取变量问题,php,url,get,Php,Url,Get,如何将“&”符号放入URL GET变量,使其成为字符串的一部分? 问题是它总是将字符串拆分为下一个变量 我怎样才能做到这一点 localhost/test.php?variable='jeans&shirts' // so it executes it like a string <?php require "connect.php"; $variable = $_GET['variable']; echo $variable; ?> localhost/t
localhost/test.php?variable='jeans&shirts' // so it executes it like a string
<?php
require "connect.php";
$variable = $_GET['variable'];
echo $variable;
?>
localhost/test.php?variable='jeans&shirts'//因此它像字符串一样执行它
产量为“牛仔裤”
除了“牛仔裤和衬衫”之外,您还需要:
// Your link would look like this:
'localhost/test.php?variable='.urlencode('jeans&shirts');
当您想要使用它时,您可以对其进行解码:
echo $variable = urldecode($_GET['variable']);
编码:
解码:
编辑:要测试,请编写此命令:
echo $url = 'localhost/test.php?variable='.urlencode('jeans&shirts');
echo '<br />';
echo urldecode($url);
这是误导。在url中使用字符串之前,需要对其进行url编码。您的第一行代码根本不应该引用
$\u GET['variable']
,而应该显示链接标记或similar@Steve很抱歉,当您发表评论时,我正在更新,因为我知道这有点误导。这对我不起作用:\n但别介意我通过排除URL中的“&”符号来解决问题。您应该始终使用urlencode()
将变量转换为查询字符串时。应该将$\u GET['variable']
输出到jeans%26shirts
。没问题,希望你能让它工作起来!这将在将来为你省去麻烦!
// Encoded
localhost/test.php?variable=jeans%26shirts
// Decoded
localhost/test.php?variable=jeans&shirts