sol
sol

Reputation: 385

PHP - parse current URL

I need to parse the current url so that, in either of these cases:

http://mydomain.com/abc/
http://www.mydomain.com/abc/

I can get the return value of "abc" (or whatever text is in that position). How can I do that?

Upvotes: 16

Views: 73402

Answers (6)

prakash
prakash

Reputation: 11

$url = 'http://www.mydomain.in/abc/';

print_r(parse_url($url));

echo parse_url($url, PHP_URL_host);

Upvotes: 1

Trung Lương
Trung Lương

Reputation: 21

<?php
$url = "http://www.mydomain.com/abc/"; //https://www... http://... https://...
echo substr(parse_url($url)['path'],1,-1); //return abc
?>

Upvotes: 2

sarah
sarah

Reputation: 105

<?function urlSegment($i = NULL) {
static $uri;
if ( NULL === $uri )
{
    $uri = parse_url( $_SERVER['REQUEST_URI'], PHP_URL_PATH );
    $uri = explode( '/', $uri );
    $uri = array_filter( $uri );
    $uri = array_values( $uri );
}
if ( NULL === $i )
{
    return '/' . implode( '/', $uri );
}
$i =  ( int ) $i - 1;
$uri = str_replace('%20', ' ', $uri);
return isset( $uri[$i] ) ? $uri[$i] : NULL;} ?>

sample address in browser: http://localhost/this/is/a/sample url

<?  urlSegment(1); //this
urlSegment(4); //sample url?>

Upvotes: 0

Maxime Pacary
Maxime Pacary

Reputation: 23021

To retrieve the current URL, you can use something like $url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];

If you want to match exactly what is between the first and the second / of the path, try using directly $_SERVER['REQUEST_URI']:

<?php

function match_uri($str)
{
  preg_match('|^/([^/]+)|', $str, $matches);

  if (!isset($matches[1]))
    return false;

  return $matches[1];  
}

echo match_uri($_SERVER['REQUEST_URI']);

Just for fun, a version with strpos() + substr() instead of preg_match() which should be a few microseconds faster:

function match_uri($str)
{
  if ($str{0} != '/')
    return false;

  $second_slash_pos = strpos($str, '/', 1);

  if ($second_slash_pos !== false)
    return substr($str, 1, $second_slash_pos-1);
  else
    return substr($str, 1);
}

HTH

Upvotes: 4

KJYe.Name
KJYe.Name

Reputation: 17169

You can use parse_url();

$url = 'http://www.mydomain.com/abc/';

print_r(parse_url($url));

echo parse_url($url, PHP_URL_PATH);

which would give you

Array
(
    [scheme] => http
    [host] => www.mydomain.com
    [path] => /abc/
)
/abc/

Update: to get current page url and then parse it:

function curPageURL() {
 $pageURL = 'http';
 if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
 $pageURL .= "://";
 if ($_SERVER["SERVER_PORT"] != "80") {
  $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
 } else {
  $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
 }
 return $pageURL;
}

print_r(parse_url(curPageURL()));

echo parse_url($url, PHP_URL_PATH);

source for curPageURL function

Upvotes: 41

anon
anon

Reputation:

Take a look at the parse_url() function. It'll break you URL into its component parts. The part you're concerned with is the path, so you can pass PHP_URL_PATH as the second argument. If you only want the first section of the path, you can then use explode() to break it up using / as a delimiter.

$url = "http://www.mydomain.com/abc/";
$path = parse_url($url, PHP_URL_PATH);
$pathComponents = explode("/", trim($path, "/")); // trim to prevent
                                                  // empty array elements
echo $pathComponents[0]; // prints 'abc'

Upvotes: 13

Related Questions