Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP get URI parts of URL

Tags:

php

uri

I have 2 strings and substitute them, so I get just URI parts.

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

I am checking for URI parts.

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

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

Then I can get segments:

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

Question:

Am I on right path or can it be done easier with $_SERVER['REQUEST_URI'] ?

Note: I don't want project in URI parts, it is sub-folder of localhost.

like image 845
Ing. Michal Hudak Avatar asked Nov 13 '13 14:11

Ing. Michal Hudak


People also ask

How to check URI segment?

$data['current_uri'] = $this->uri->segment(1); $this->load->view('header', $data); in each of my controllers and then in the header. php file, I check the $current_uri variable to determine which part of the navigation should be highlighted.

How to get last element from URL in php?

$basepath = implode('/', array_slice(explode('/', $_SERVER['SCRIPT_NAME']), 0, -1)) . '/'; $uri = substr($_SERVER['REQUEST_URI'], strlen($basepath)); if (strstr($uri, '? ')) $uri = substr($uri, 0, strpos($uri, '? ')); $url = trim($uri, '/');


1 Answers

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"

}

Also can get it this way:

$URL = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$URI = "$_SERVER[REQUEST_URI]";
like image 161
Mike Q Avatar answered Oct 06 '22 07:10

Mike Q