PHP parsing URL (parse_url) parameter array (PARSE_STR) _php Tutorial

Source: Internet
Author: User
Tags function prototype how to use curl
Get the current URL through PHP, if you need to extract the parameters in the URL, how to do it? This process is actually quite simple, using PHP's two built-in functions can be completed smoothly, namely the Parse_url and PARSE_STR functions. These two functions are briefly described below and examples show how to extract the parameters in the URL.

(1) Parse_url (PHP 4, PHP 5)-Parses the URL, returns its constituent parts, and the function prototype is as follows:

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

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

This function is not used to validate the legitimacy of a given URL, but to decompose it into the parts 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 Specify Php_url_scheme, Php_url_host, Php_url_port, Php_url_user, Php_url_pass, Php_url_path, Php_url_query, or PHP_ Url_fragment one of the strings that gets the specified part of the URL. (An integer value is returned, except when specified as Php_url_port).

return value

A url,parse_url () that is severely unqualified may return FALSE.

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

Scheme-such as HTTP
Host
Port
User
Pass
Path
Query-in question marks? After
Fragment-After 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 is required. Specifies the string to parse.

Array is optional. Specifies the array name for 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 the function. If enabled, the variable is converted by addslashes () before Parse_str () is resolved.

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

 

The results are printed as follows:

Array
(
[Scheme] = http
[Host] = www.phpernote.com
[Path] =/ad.php
[Query] = id=325&action=index&page=3
)
Array
(
[id] = 325
[Action] = index
[Page] = 3
)

Articles you may be interested in

    • How to use Curl's post submission data in PHP and get a summary of how to get Web page data
    • PHP addresses URL encoding problem functions UrlEncode (), UrlDecode (), Rawurlencode (), Rawurldecode ()
    • PHP string Escape function (Addslashes,stripslashes) detailed
    • PHP Gets the current page full URL address function, including parameters
    • PHP Curl Bulk Multi-threaded open URL class
    • The idea and implementation method of PHP generating short URLs
    • PHP compressed HTML page code reduces network data transfer, clear spaces, tabs, comment tags
    • PHP uses Curl Functions to crawl Web pages and download files in multiple threads

http://www.bkjia.com/PHPjc/779411.html www.bkjia.com true http://www.bkjia.com/PHPjc/779411.html techarticle get the current URL through PHP, if you need to extract the parameters in the URL, how to do it? This process is actually quite simple, using PHP's two built-in functions can be completed successfully, namely par ...

  • Related Article

    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: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.