Shin Shin - 3 months ago 7
PHP Question

PHP: Get parameter from encoded url

I have this url:

/?goto=%2Fr%2Faccount%2Findex%2Ecfm%3Fsite_id%3D87211


How can I
GET
site_id from this url?

Answer

If you know your url's will always be in this structure, you can parse it out with RegEx like so:

$url = '/?goto=%2Fr%2Faccount%2Findex%2Ecfm%3Fsite_id%3D87211';
preg_match('/site_id\%3D(\d+)/', $url, $matches);
$siteId = $matches[1];

It's searching for anything that matches site_id%3D and the numeric value that follows that.

Comments