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

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

?>

输出是'牛仔裤'

而不是“牛仔裤和衬衫”

最佳答案

你会想 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

关于$_GET 变量问题中的 PHP "&"字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26721169/

相关文章:

rest - GET 或 POST 生成 token

php - 使用php codeigniter连接到远程服务器数据库

php - 确定 PHP 是否存在 mySQL 错误

django 'url'模板标签错误

ruby-on-rails - Rails : dynamically generated path is adding a period and the id at the end

PHP slimframework GET 请求不返回任何内容

PHP:strlen 返回字符长度而不是字节长度

phpMyAdmin 不会在数据库中正确显示 Unicode (utf-8) 字符,但在 Web 中正确显示

python - 如何使用 Python 将 URL 查询字符串转换为元组列表?

javascript - 在继续之前确保异步 GET/POST 请求已完成