如何将‘&’符号放在URL获取变量中,因此它是字符串的一部分?
问题是它总是将字符串拆分为下一个变量。
我怎样才能做到这一点呢?

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

<?php

require "connect.php";

$variable = $_GET['variable'];

echo $variable;

?>

输出是“牛仔裤”
而不是“牛仔裤和衬衫”

最佳答案

您需要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

09-26 18:43
查看更多