Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

SimonAngatia's avatar

How do I extract an id from a dynamically generated URL?

I dynamically generate URLs based on the current session. Example below. How do I extract the language_code, device_id, and session_id for example? Then the URL is not my website's URL. I just generate it dynamically in one of my controllers.

 https://<host>?l=<LANGUAGE_CODE>&d=<DEVICE_ID>&s=<SESSION_ID>

0 likes
1 reply
rodrigo.pedra's avatar
Level 56

Use PHP's parse_url and parse_str functions:

$url = 'https://example.com?l=en&d=1234&s=abcd';

$query = parse_url($url)['query'] ?? '';

parse_str($query, $parameters);

$languageCode = $parameters['l'] ?? '';
$deviceId = $parameters['d'] ?? '';
$sessionId = $parameters['s'] ?? '';

dd($languageCode, $deviceId, $sessionId);

References:

https://www.php.net/manual/en/function.parse-url.php

https://www.php.net/manual/en/function.parse-str.php

Please or to participate in this conversation.