3v4l.org

run code in 500+ PHP versions simultaneously
<?php // Sample URLs. In practice there'd just be one which comes from $_SERVER['REQUEST_URI'] $urls = [ 'https://example.com', 'https://example.com/', 'https://example.com/en', 'https://example.com/en/', 'https://example.com/en/something', 'https://example.com/en/something/cheese', 'https://example.com/en/something/cheese/stuff', ]; foreach($urls as $url){ $urlParts = parse_url($url); $path = $urlParts['path'] ?? '/'; $pathParts = array_values(array_filter(explode('/', $path))); $lang = $menu = $page = $key = null; echo 'Url tested: ' . $url; echo PHP_EOL; if(!$pathParts){ echo 'Home'; }else{ // I would probably write this with a switch on the count(pathParts), or do some better // parsing here maybe, but this just shows what could happen @[0 => $lang, 1 => $menu, 2 => $page, 3 => $key] = $pathParts; echo sprintf('Lang=%1$s, Menu=%2$s, Page=%3$s, Key=%4$s', $lang, $menu, $page, $key); } echo PHP_EOL, PHP_EOL; }
Output for 8.1.0 - 8.1.34, 8.2.0 - 8.2.30, 8.3.0 - 8.3.30, 8.4.1 - 8.4.18, 8.5.0 - 8.5.3
Url tested: https://example.com Home Url tested: https://example.com/ Home Url tested: https://example.com/en Lang=en, Menu=, Page=, Key= Url tested: https://example.com/en/ Lang=en, Menu=, Page=, Key= Url tested: https://example.com/en/something Lang=en, Menu=something, Page=, Key= Url tested: https://example.com/en/something/cheese Lang=en, Menu=something, Page=cheese, Key= Url tested: https://example.com/en/something/cheese/stuff Lang=en, Menu=something, Page=cheese, Key=stuff

preferences:
77.63 ms | 1046 KiB | 4 Q