PHP Parse URL (parse

Source: Internet
Author: User
Tags array function prototype functions html page implement php and string variable

Get the current URL through PHP, if you need to extract the parameters in the URL how to operate it? This process is actually quite simple, using PHP's two built-in functions can be successfully completed, that is, Parse_url and PARSE_STR functions. The two functions are briefly described below and examples show how to extract the parameters from the URL.

(1) Parse_url (PHP 4, PHP 5)-parse URL, return its component, function prototype as follows:

Mixed Parse_url (string $url [, int $component =-1])

This function resolves a URL and returns an associative array containing the various components that appear in the URL.

This function is not used to verify the legality of a given URL, but to decompose it into the sections listed below. An incomplete URL is also accepted, and Parse_url () tries to parse it as correctly as possible.

Parameter description

The URL to parse for the URL. Invalid characters will be replaced with _.

component Specifies Php_url_scheme, Php_url_host, Php_url_port, Php_url_user, Php_url_pass, Php_url_path, Php_url_query, or PHP_ One of the url_fragment to get a string for the part specified in the URL. (An integer value is returned, except when specified as Php_url_port.)

return value

The severely unqualified url,parse_url () may return FALSE.

If the component argument is omitted, an associative array array is returned, and at least one element is currently in the array. There are several possible keys in the array:

Scheme-such as HTTP
Query-at question mark? After
Fragment-After the hash symbol #

If the component parameter is specified, Parse_url () returns a string (or returns an integer when specified as Php_url_port) instead of an array. If the component specified in the URL does not exist, NULL will be returned.

(2) Parse_str-parse the query string into a variable, the function prototype is as follows:

Parse_str (String,array)

Parameter description

String required. Specify the string to parse.

Array is optional. Specify the array name of the stored variable. This parameter indicates that the variable is stored in the array. If the array parameter is not set, the variable set by the function overrides the variable with the same name.

Note: The MAGIC_QUOTES_GPC setting in php.ini affects the output of this function. If enabled, the variable is converted by addslashes () before Parse_str () is parsed.

(3) The specific extraction of the parameters in the URL examples are as follows:

$url = ' ';
$urlarr =parse_url ($url);
Parse_str ($urlarr [' query '], $parr);
Print_r ($urlarr);
Print_r ($parr);

The results of the printing are as follows:

[Scheme] => http
[Host] =>
[Path] =>/ad.php
[Query] => id=325&action=index&page=3
[ID] => 325
[Action] => index
[Page] => 3

Articles that you may be interested in

    • Summary of methods for using Curl post submission data and get access to Web page data in PHP
    • PHP Curl Batch multi-threaded open URL class
    • PHP gets the full URL address function of the current page, including parameters
    • PHP string Escape function (Addslashes,stripslashes) detailed
    • PHP addresses URL encoding problem functions UrlEncode (), UrlDecode (), Rawurlencode (), Rawurldecode ()
    • How to generate short URLs in PHP and how to implement them
    • PHP compressed HTML page code to reduce network data transfer, clear spaces, tabs, comment marks
    • PHP uses Curl functions to implement multi-threaded crawl Web pages and download files

Related Article

Alibaba Cloud 10 Year Anniversary

With You, We are Shaping a Digital World, 2009-2019

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.