首页 > 解决方案 > 抓取带有特殊字符的 URL 参数

问题描述

我使用以下方法从 URL 中获取参数:

<?php echo $_GET['compname']; ?>

我刚刚注意到一些参数包含特殊字符,所以我尝试使用以下内容:

<?php echo urlencode($_GET['compname']); ?>

但是输出看起来像这样:

COMPANYNAME+LLC+LTD

如果公司名称有特殊字符(例如:COMPANY & SONS),输出将如下所示:

COMPANY+

我尝试使用这样的 HTMLENTITIES:

<?php echo urlencode(htmlentities($_GET['compname'])); ?>

但我得到相同的结果

COMPANY+

我尝试使用以下网站中的示例:

https://www.php.net/manual/en/function.htmlentities.php

这是我的尝试:

<?php echo urlencode(htmlentities($_GET['compname'], ENT_QUOTES)); ?>

但我得到了相同的结果:

COMPANY+

我需要让结果看起来像这样:

COMPANY & SONS

我做错了什么,我该如何解决?

标签: phpurlencodehtml-entities

解决方案


<?php 
$_GET['compname']="this+is+a+test+for+you";
echo urldecode($_GET['compname']); // decode this is a test for you
?>

在这里阅读更多https://www.php.net/manual/en/function.urldecode.php


推荐阅读