PHP - 解析当前 URL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5598480/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 21:56:07  来源:igfitidea点击:

PHP - parse current URL

phpparsing

提问by sol

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

我需要解析当前 url,以便在以下任一情况下:

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?

我可以获得“abc”的返回值(或该位置的任何文本)。我怎样才能做到这一点?

回答by u476945

You can use parse_url();

您可以使用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:

更新:获取当前页面 url,然后解析它:

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

curPageURL 函数的源代码

回答by AgentConundrum

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_PATHas 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.

看一下parse_url()功能。它会将您的 URL 分解为其组成部分。您关心的部分是路径,因此您可以将其PHP_URL_PATH作为第二个参数传递。如果您只需要路径的第一部分,则可以将explode()/用作分隔符来分解它。

$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'

回答by Frosty Z

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

要检索当前 URL,您可以使用类似 $url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];

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

如果要完全匹配路径的第一个和第二个 / 之间的内容,请尝试直接使用$_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:

只是为了好玩,一个带有strpos()+substr()而不是preg_match()它的版本应该快几微秒:

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

HTH

回答by Trung L??ng

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

回答by prakash

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

print_r(parse_url($url));

echo parse_url($url, PHP_URL_host);

回答by sarah

<?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/sampleurl

浏览器中的示例地址:http://localhost/this/is/a/sampleurl

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