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

Pet*_*lak 3 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;

?>
Run Code Online (Sandbox Code Playgroud)

输出是'牛仔裤'

而不是'牛仔裤和衬衫'

Ras*_*att 8

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

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

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

echo $variable = urldecode($_GET['variable']);
Run Code Online (Sandbox Code Playgroud)

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);
Run Code Online (Sandbox Code Playgroud)

你的结果将是:

// Encoded
localhost/test.php?variable=jeans%26shirts
// Decoded
localhost/test.php?variable=jeans&shirts
Run Code Online (Sandbox Code Playgroud)