ray-solomon
asked on
optimize a Preg_match rule
I made this script to get the url and extract the value of the info variable. It works great, but I need to know if there is a better way to rewrite this to be a smaller amount of code. I think it is quite bulky looking.
It can be tested by simply using a url like this:
http://www.domain.com/somepage.ext?info=testdata&blah=moretests
It should only echo: testdata
$s = preg_match("/\\?info=(.*)( [&])/i", $_SERVER['REQUEST_URI'], $matches);
if ($s !== false)
{
$tmp = str_replace('&', '', $matches[0]);
echo $info = str_replace('?info=', '', $tmp);
}
It can be tested by simply using a url like this:
http://www.domain.com/somepage.ext?info=testdata&blah=moretests
It should only echo: testdata
$s = preg_match("/\\?info=(.*)(
if ($s !== false)
{
$tmp = str_replace('&', '', $matches[0]);
echo $info = str_replace('?info=', '', $tmp);
}
ASKER
I get this error:
Notice: Undefined offset: 1 in....
I have php 5 with register_globals=off if that helps.
Notice: Undefined offset: 1 in....
I have php 5 with register_globals=off if that helps.
ASKER
Here is what is contained in the parse_url array besides the path i modified
Array
(
[path] => /whatever/whatever......
[query] => info=testdata&blah=moretes ts
)
Array
(
[path] => /whatever/whatever......
[query] => info=testdata&blah=moretes
)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
$pieces = parse_url($url);
preg_match('/[&?]info=([^&
echo $match[1];