Link to home
Start Free TrialLog in
Avatar of ray-solomon
ray-solomonFlag for United States of America

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);
            }
Avatar of VoteyDisciple
VoteyDisciple

I'd suggest using the parse_url() method to get partway there.

$pieces = parse_url($url);
preg_match('/[&?]info=([^&]+)/', $pieces['query'], $match);
echo $match[1];
Avatar of ray-solomon

ASKER

I get this error:

Notice: Undefined offset: 1 in....

I have php 5 with register_globals=off if that helps.
Here is what is contained in the parse_url array besides the path i modified

Array
(
    [path] => /whatever/whatever......
    [query] => info=testdata&blah=moretests
)
ASKER CERTIFIED SOLUTION
Avatar of VoteyDisciple
VoteyDisciple

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial