简体   繁体   中英

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

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.

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();

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