简体   繁体   中英

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 first 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

您不能只使用$ _GET ['section']和$ _GET ['tool']吗?

'section=(.+?).*?&tool=(.+?)'应该起作用,然后检查组1和2的值

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM