PHP gets URL URI parts

I have 2 lines and replace them, so I only get parts of the URI.

$base_url = 'http://localhost/project/'; $url = 'http://localhost/project/controller/action/param1/param2'; 

I am checking parts of a URI.

 $only_segments = str_replace($base_url, '', $real_url); 

Basically I do: {url} - {base_url} = {only_segments}

Then I can get the segments:

 $parts = explode('/', $only_segments); print_r($parts); 

Question:

Am I on the right track, or can this be done easier with $ _ SERVER ['REQUEST_URI'] ?

Note. I do not want project in parts of the URI, it is a subfolder of localhost.

+6
source share
2 answers

Take a look at parse_url() . This will give you most of the way. All you have to do is delete the part of the path that is part of your base URL.

 print_r(parse_url('http://localhost/project/controller/action/param1/param2')); Array ( [scheme] => http [host] => localhost [path] => /project/controller/action/param1/param2 ) 
+10
source

A more complete example:

 var_dump(parse_url(http://lambo-car-dealer.com/Lambo/All+Models/2018/?limits=10&advance_options=no)); 

output:

 array(4) { ["scheme"]=> string(4) "http" ["host"]=> string(48) "lambo-car-dealer.com" ["path"]=> string(44) "/Lambo/All+Models/2018/" ["query"]=> string(121) "limits=10&advance_options=no" 

}

You can also get it as follows:

 $URL = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; $URI = "$_SERVER[REQUEST_URI]"; 
0
source

Source: https://habr.com/ru/post/958000/


All Articles