How to get parameter from url split by colon (:)

283 Views Asked by At

I have this url: "http://example.com/search/dep_city:LON/arr_city=NYC". How can i get the values "LON" and "NYC" from this url in PHP? This would be easy if url would be in format "dep_city=LON" by using "$_GET['dep_city']" but in this case i am confused how to do this. Thanks in advance for your time and help.

1

There are 1 best solutions below

2
Rentabear On BEST ANSWER

Get the URL from the server, explode it into parts. Check the index of : or = for each part, parse it into an array.

<?php
$url = substr( $_SERVER["REQUEST_URI"], 1, strlen($_SERVER["REQUEST_URI"]));
$vars = explode("/", $url);

foreach($vars as $var){

    $indexEquals = strpos($var, "=");

    if($indexEquals === false){
        $indexColon = strpos($var, ":");
        $part1 = substr($var, 0, $indexColon);
        $part2 = substr($var, $indexColon+1, strlen($var));
    }else{
        $part1 = substr($var, 0, $indexEquals);
        $part2 = substr($var, $indexEquals+1, strlen($var));
    }

    $params[$part1] = $part2;
}

echo json_encode($params);
die();