无法从php中的url检索括号值?

问题描述 投票:0回答:3

我从url检索括号值时出现问题。我正在使用CodeIgniter Framework开发一个PHP项目。

我有像这样的mvc url格式 - http://localhost/controller/action/id/name

然后我传递了这样的价值 - http://localhost/controller/action/2/test%20(test)

也像这样 - 然后我传递了这样的值:

http://localhost/controller/action/2/test+(test)

所以我的动作功能是这样的参数 -

function action($id,$name)
{
   $name = urldecode($name)
   //I find the index of start "t"
   echo strpos($name,'t'); //this is working find
   echo strpos($name,'('); // this is outputing nothing
   echo $name; // this is outputing test (test)
}

问题是什么 ?为什么我找不到索引“(”。实际上我和使用该值从数据库中检索记录。但括号不适用于php。我正确显示输出。但找不到它的索引。

php codeigniter
3个回答
1
投票

最后我找到了解决方案。这是Codeigniter Framework的问题。在urlencoding之前,我用“[”和“]”替换了所有“(”和“)”。然后我在urldecoding之后用“(”和“)”替换了所有“[”和“]”。阅读本文后,您将得到答案。 http://qubitlogs.com/PHP/2013/01/24/parentheses-in-urls-not-working-codeigniter-datamapper-orm/。感谢大家帮助我。


0
投票

尝试使用preg_match

<?php
preg_match('/\((.*?)\)/',urldecode($name),$a);
echo $a[1];

0
投票

试试这个

<?php
$url = 'localhost/melomap_music/singers/details/760/test%20(test)';
$string = urldecode($url);
$pattern1 = '/[)]/';
$pattern2 = '/[(]/';
preg_match($pattern1, $string, $matches, PREG_OFFSET_CAPTURE);
preg_match($pattern2, $string, $matches2, PREG_OFFSET_CAPTURE);
$start = $matches2[0][1]; //49
$end = $matches[0][1]; //54
?>
© www.soinside.com 2019 - 2024. All rights reserved.