[英]preg_match - extracting string from url
我有一個這樣的URL:
http://localhost/adminator/index.php?section = 1portal&tool = 2firmy和http://localhost/adminator/index.php?section = 1portal&tool = 2firmy&passedID = 26
並且我希望能夠提取SECTION和TOOL參數。
我想出了這個:
preg_match('/(.*)(section=)(.*)(&tool=)(.*)/', $_SERVER['HTTP_REFERER'], $matchesarray);
echo $section = $matchesarray[3].'<br />';
echo $tool = $matchesarray[5];
但這僅適用於第一個URL,不適用於第二個URL,並且比我有:
preg_match('/(.*)(section=)(.*)(&tool=)(.*)(&)(.*)/', $_SERVER['HTTP_REFERER'], $matchesarray);
echo $section = $matchesarray[3].'<br />';
echo $tool = $matchesarray[5];
這僅適用於第二個網址,不適用於第一個。
如何在兩種情況下都可以使用? 謝謝。
$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的值
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.