I am having a problem a problem with retrieving parenthesis value from url. I am developing a PHP project using CodeIgniter Framework.
I have a mvc url format like this - http://localhost/controller/action/id/name
Then I passed the value like this - http://localhost/controller/action/2/test%20(test)
And like this as well - Then I passed the value like this:
So my action function is like this with parameters-
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)
}
What is the problem ? Why cannot I find index of "(". Actually I and retrieving record from database using that value. But parenthesis is not working well with php. I showing output correctly. But cannot find index of it.
Try this
<?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
?>
Try like this with preg_match:
<?php
preg_match('/\((.*?)\)/',urldecode($name),$a);
echo $a[1];
Finally I found the solution. That is the issue with Codeigniter Framework. I replaced all "(" and ")" with "[" and "]" before urlencoding. Then I replaced all "[" and "]" with "(" and ")" after urldecoding. You will get the answer after you read this article. http://qubitlogs.com/PHP/2013/01/24/parentheses-in-urls-not-working-codeigniter-datamapper-orm/ . Thank everyone for helping me.