16

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?

sol
  • 385
  • 2
  • 10
  • 18
  • Check out the built-in PHP function [`parse_url`](http://php.net/manual/en/function.parse-url.php). That should do what you are looking for. – DanielJay Apr 08 '11 at 17:12

6 Answers6

41

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

KJYe.Name
  • 16,969
  • 5
  • 48
  • 63
  • 1
    Thank you, but is there a way to get the current url automatically from the browser? I can't hard-code it. – sol Apr 08 '11 at 17:18
  • I have a question, what if you don't use http:// in the url what if i have just mydomain.com/abc/, i tried but it returns only path. – lonerunner Dec 16 '14 at 21:06
  • 1
    @AleksandarĐorđević I had to remove the PHP_URL_PATH, and use the default argument (-1) so that it would return an array like in the answer. – Brad G Jul 12 '16 at 18:34
13

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'
AgentConundrum
  • 20,288
  • 6
  • 64
  • 99
4

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

Maxime Pacary
  • 22,336
  • 11
  • 85
  • 113
2
<?php
$url = "http://www.mydomain.com/abc/"; //https://www... http://... https://...
echo substr(parse_url($url)['path'],1,-1); //return abc
?>
1
$url = 'http://www.mydomain.in/abc/';

print_r(parse_url($url));

echo parse_url($url, PHP_URL_host);
tom redfern
  • 30,562
  • 14
  • 91
  • 126
prakash
  • 11
  • 1
0
<?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?>
sarah
  • 103
  • 2
  • 12