在php中的querystring中传递#作为文本

时间:2013-11-21 14:15:51

标签: php query-string querystringparameter

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

但是在上面,%23在网址#之前是Gas,并且在php之后无法读取所有内容。

当我尝试获取s&的价值时来自查询字符串的o,我没有为os获取任何值,只有Asco+8214G30+3%252F4%5C%5C%5C%220%252F5返回,但它应该返回Asco+8214G30+3%252F4%5C%5C%5C%220%252F5%23+Gas+Shutoff+Valve

我使用httaccess重写来制作这个网址     RewriteRule ^([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']));

3 个答案:

答案 0 :(得分:2)

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

解释如下: Can I read the hash portion of the URL on my server-side application (PHP, Ruby, Python, etc.)?

你想要得到这个#你必须使用像Slugify之类的库来强制/ urlize你的网址这将编码“奇怪的”字符。

示例:

use Cocur\Slugify\Slugify;

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

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

答案 1 :(得分:0)

尝试使用rawurlencode()函数。

答案 2 :(得分:0)

我在使用下面的代码对网址进行urlencoding之前,将#转换为%23,从而更正了这一点,

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