Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP-解析当前URL_Php_Parsing - Fatal编程技术网

PHP-解析当前URL

PHP-解析当前URL,php,parsing,Php,Parsing,我需要解析当前url,以便在以下任一情况下: http://mydomain.com/abc/ http://www.mydomain.com/abc/ 我可以得到“abc”的返回值(或该位置的任何文本)。我该怎么做呢?你可以使用 那会给你什么 Array ( [scheme] => http [host] => www.mydomain.com [path] => /abc/ ) /abc/ 更新:获取当前页面url,然后对其进行解析: funct

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

http://mydomain.com/abc/
http://www.mydomain.com/abc/
我可以得到“abc”的返回值(或该位置的任何文本)。我该怎么做呢?

你可以使用

那会给你什么

Array
(
    [scheme] => http
    [host] => www.mydomain.com
    [path] => /abc/
)
/abc/
更新:获取当前页面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);

看看这个函数。它会将您的URL分解为其组成部分。您关心的部分是路径,因此可以将
PHP\u URL\u 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'

要检索当前URL,可以使用类似以下内容:$\u SERVER['http\u HOST']。$\u SERVER['REQUEST\u URI']

如果要精确匹配路径的第一个/和第二个/之间的内容,请尝试直接使用
$\u SERVER['REQUEST\u 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']);
HTH


浏览器中的示例地址:url




谢谢,但是有没有办法从浏览器中自动获取当前url?我不能硬编码它。我有一个问题,如果你不在url中使用http://怎么办如果我只有mydomain.com/abc/,我试过了,但它只返回路径。@AleksandarĐ或đević我必须删除PHP_url_路径,并使用默认参数(-1),这样它将返回一个与答案类似的数组。检查内置PHP函数。注意:
echo parse_url($url)['path']
返回
/abc/
,然后
substr(…,1,-1)
删除前面和后面的斜杠
/
,因此最终结果变成
abc
,您还可以使用trim()的可选参数<代码>修剪(解析url($url)['path'],'/')
<?php

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

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

  return $matches[1];  
}

echo match_uri($_SERVER['REQUEST_URI']);
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);
}
<?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;} ?>
<?  urlSegment(1); //this
urlSegment(4); //sample url?>
<?php
$url = "http://www.mydomain.com/abc/"; //https://www... http://... https://...
echo substr(parse_url($url)['path'],1,-1); //return abc
?>
$url = 'http://www.mydomain.in/abc/';

print_r(parse_url($url));

echo parse_url($url, PHP_URL_host);