How does php parse url? Introduction to 5 ways to parse URLs

青灯夜游
Release: 2023-04-09 09:38:02
forward
8607 people have browsed it

How does php parse url? Introduction to 5 ways to parse URLs

Several ways for PHP to parse URLs

##1. Use the $_SERVER built-in array variable

Visit:

http://localhost/test.php?m=admin&c=index&a=lists&catid=1&page=1

//URL的参数
echo $_SERVER['QUERY_STRING'];
返回:
m=admin&c=index&a=lists&catid=1&page=1
//包含文件名
echo $_SERVER["REQUEST_URI"];
Copy after login
Return :


/test.php?m=admin&c=index&a=lists&catid=1&page=1
Copy after login

2. Use the pathinfo built-in function

echo "
";
$url = 'http://localhost/test.php?m=admin&c=index&a=lists&catid=1&page=1#top';
var_export(pathinfo($url));
Copy after login

Return:

array (
  'dirname' => 'http://localhost',
  'basename' => 'test.php?m=admin&c=index&a=lists&catid=1&page=1#top',
  'extension' => 'php?m=admin&c=index&a=lists&catid=1&page=1#top',
  'filename' => 'test',
)
Copy after login

3. Use the parse_url built-in function

echo "
";
$url = 'http://localhost/test.php?m=admin&c=index&a=lists&catid=1&page=1#top';
var_export(parse_url($url));
Copy after login

Return:

array (
  'scheme' => 'http',
  'host' => 'localhost',
  'path' => '/test.php',
  'query' => 'm=admin&c=index&a=lists&catid=1&page=1',
  'fragment' => 'top',
)
Copy after login

4. Use basename built-in function

echo "
";
$url = 'http://localhost/test.php?m=admin&c=index&a=lists&catid=1&page=1#top';
var_export(basename($url));
Copy after login

Return:

test.php?m=admin&c=index&a=lists&catid=1&page=1#top
Copy after login

5. Regular matching

echo "
";
$url = 'http://localhost/test.php?m=admin&c=index&a=lists&catid=1&page=1#top';
preg_match_all("/(\w+=\w+)(#\w+)?/i",$url,$match);
var_export($match);
Copy after login

Returns:

array (
  0 => 
  array (
    0 => 'm=admin',
    1 => 'c=index',
    2 => 'a=lists',
    3 => 'catid=1',
    4 => 'page=1#top',
  ),
  1 => 
  array (
    0 => 'm=admin',
    1 => 'c=index',
    2 => 'a=lists',
    3 => 'catid=1',
    4 => 'page=1',
  ),
  2 => 
  array (
    0 => '',
    1 => '',
    2 => '',
    3 => '',
    4 => '#top',
  ),
)
Copy after login

Common url processing methods

/**
 * 将字符串参数变为数组
 * @param $query
 * @return array
 */
function convertUrlQuery($query)
{
    $queryParts = explode('&', $query);
    $params = array();
    foreach ($queryParts as $param) {
        $item = explode('=', $param);
        $params[$item[0]] = $item[1];
    }
    return $params;
}

/**
 * 将参数变为字符串
 * @param $array_query
 * @return string
 */
function getUrlQuery($array_query)
{
    $tmp = array();
    foreach ($array_query as $k => $param) {
        $tmp[] = $k . '=' . $param;
    }
    $params = implode('&', $tmp);
    return $params;
}
Copy after login

Example:


echo "
";
$url = 'http://localhost/test.php?m=admin&c=index&a=lists&catid=1&page=1#top';
$arr = parse_url($url);
$arr_query = convertUrlQuery($arr['query']);
var_export($arr_query);
Copy after login

Return:


array (
  'm' => 'admin',
  'c' => 'index',
  'a' => 'lists',
  'catid' => '1',
  'page' => '1',
)
Copy after login
var_export(getUrlQuery($arr_query));
Copy after login

Return:


m=admin&c=index&a=lists&catid=1&page=1
Copy after login
Recommended related tutorials: "

PHP Tutorial"

The above is the detailed content of How does php parse url? Introduction to 5 ways to parse URLs. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template