PHP"&" $ _GET变量问题中的字符

时间:2014-11-03 19:03:36

标签: 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;

?>

输出是&#39;牛仔裤&#39;

代替&#39;牛仔裤和衬衫&#39;

1 个答案:

答案 0 :(得分:7)

您需要urlencode()字符串:

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

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

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

ENCODE: 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