简体   繁体   中英

PHP get parameter from variable with url

I would like to get parameter from variable that holds URL.
I got the following variable: $lastPage that holds the URI of the previous page.

I would like to get a parameter from it.
I got the following script in javascript (which I use for something else)

function getUrlVar(key)
{
    var result = new RegExp(key + '=([^&]*)', 'i').exec(window.location.search);
    return result && unescape(result[1]) || '';
}

I want something like that but for PHP.

Thank you very much

$url = 'http://php.net/manual/en/function.parse-url.php?id=my_awesome_id';
$arr = parse_url($url);
parse_str($arr['query']);
echo $id; // my_awesome_id

Reference:

parse_str

parse_url

You can use preg_match() or preg_match_all() in php which will give you the regular expression functionality.

http://php.net/manual/en/function.preg-match.php

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