preg_match - extracting string from url
I've got an URL's like this:
http://localhost/adminator/index.php?section=1portal&tool=2firmy and http://localhost/adminator/index.php?section=1portal&tool=2firmy&passedID=26
and I want to be able to extract the SECTION and TOOL parameters.
I've came up with this:
preg_match('/(.*)(section=)(.*)(&tool=)(.*)/', $_SERVER['HTTP_REFERER'], $matchesarray);
echo $section = $matchesarray[3].'<br />';
echo $tool = $matchesarray[5];
But this works only for the f开发者_开发技巧irst URL, not the second, and than I have this:
preg_match('/(.*)(section=)(.*)(&tool=)(.*)(&)(.*)/', $_SERVER['HTTP_REFERER'], $matchesarray);
echo $section = $matchesarray[3].'<br />';
echo $tool = $matchesarray[5];
And this only works for the second url, not the first.
How can I make it work in both cases? Thanks.
$url = 'http://localhost/adminator/index.php?section=1portal&tool=2firmy&passedID=26';
$url = parse_url($url, PHP_URL_QUERY);
parse_str($url, $output);
echo $output['section']; // 1portal
echo $output['tool']; // 2firmy
Can't you just use $_GET['section'] and $_GET['tool']?
'section=(.+?).*?&tool=(.+?)'
should work, then check group 1 and 2 for the value
精彩评论