【问题标题】:Pass # as text in querystring in php在 php 中将 # 作为查询字符串中的文本传递
【发布时间】:2013-12-06 01:26:37
【问题描述】:

我有这个网址https://www.industrialstores.com/search_results/Asco+8214G30+3%252F4%5C%5C%5C%220%252F5%23+Gas+Shutoff+Valve/46

但在上面,%23 在 url 中 Gas 之前是 #,并且在 php 中使之后的所有内容不可读。

当我尝试从查询字符串中获取so 的值时,我没有得到os 的任何值,只有Asco+8214G30+3%252F4%5C%5C%5C%220%252F5 返回但它应该返回@987654332 @

我正在使用 httaccess 重写来制作这个 url 重写规则 ^([a-zA-Z0-9_-]{3,100})/([^/]+)/([^/]+)?$ index.php?page=$1&s=$2&o=$3 [L ]

获取查询字符串值的代码为:

if(isset($_GET['s'])){

$search_keyword=str_replace("_"," ",$_GET['s']);
}
if(isset($_GET['o']) && $_GET['o'] !=''){
$searchin = $_GET['o'];
}

并设置查询字符串值如下:

$keyword = urlencode(str_replace("/","%2F",$_REQUEST['search_keyword']));

【问题讨论】:

标签: php query-string querystringparameter


【解决方案1】:

# 之后不能检索任何内容,任何浏览器都会删除,因为这是一个锚点。

这在以下位置进行了解释: Can I read the hash portion of the URL on my server-side application (PHP, Ruby, Python, etc.)?

你想得到这个#你必须使用像Slugify这样的库来对你的url进行slugify/urlize,这将编码“奇怪的”字符。

例子:

use Cocur\Slugify\Slugify;

$slugify = new Slugify();//for iconv translit
echo $slugify->slugify('Hello World!'); // hello-world

我认为这比让浏览器做一个简单的url编码要好。

【讨论】:

    【解决方案2】:

    尝试使用 rawurlencode() 函数。

    【讨论】:

    • 是的。此函数转义特殊字符。在目标代码上,使用 rawurldecode。 echo rawurlencode('123#321'); // 123%23321 - %23 是十六进制的#
    • # 在 url industrialstores.com/search_results/… 中已经以“%23”十六进制形式出现,但是当我对其进行解码时,不会返回 # 之后的所有文本
    • 原文是什么?当我尝试时:[code]echo rawurldecode("Asco+8214G30+3%252F4%5C%5C%5C%220%252F5%23+Gas+Shutoff+Valve/46"); php返回:[code]Asco+8214G30+3%2F4\\\"0%2F5#+Gas+Shutoff+Valve/46
    • 原文为 Asco 8214G30 3/4\"0/5# Gas Shutoff Valve
    • 你可以去这里industrialstores.com 把这段文字放在搜索框中然后点击搜索按钮,没有找到记录在这个页面上,我正在将这个查询字符串参数传递给ajax,你可以检查在ajax调用中,参数错误
    【解决方案3】:

    我通过在使用下面的代码对 url 进行 urlencoding 之前将 # 转换为 %23 来纠正这个问题,

    str_replace("#","%23",$_REQUEST['search_keyword']); 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多