Home > Backend Development > PHP Tutorial > PHP parses url (parse_url) parameters into array (parse_str)_PHP tutorial

PHP parses url (parse_url) parameters into array (parse_str)_PHP tutorial

WBOY
Release: 2016-07-13 10:28:59
Original
1015 people have browsed it

The current url is obtained through php. What should I do if I need to extract the parameters in the url? This process is actually quite simple and can be completed successfully using two built-in functions of PHP, namely parse_url and parse_str functions. The following will give a brief description of these two functions and use examples to illustrate how to extract the parameters in the url.

(1) parse_url (PHP 4, PHP 5) — Parse URL and return its components. 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 verify the validity of the given URL, but to break it down into the parts listed below. Incomplete URLs are also accepted and parse_url() will try to parse them as correctly as possible.

Parameter description

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

component Specify one of 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 to get the string of the specified part of the URL. (Except when specified as PHP_URL_PORT, an integer value will be returned).

Return value

For severely unqualified URLs, parse_url() may return FALSE.

If the component parameter is omitted, an associative array array will be returned, and at least one element will currently be in the array. The possible keys in the array are:

scheme - like http
host
port
user
pass
path
query - after the question mark ?
fragment - after the hash symbol #

If the component argument is specified, parse_url() returns a string (or an integer when specified as PHP_URL_PORT) instead of an array. If the specified component 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. Specifies the string to parse.

array optional. Specifies the name of the array to store the variable. This parameter instructs the variable to be stored in an array. If the array parameter is not set, variables set by this function will overwrite variables already set by the same name.

Note: The magic_quotes_gpc setting in php.ini affects the output of this function. If enabled, variables are transformed by addslashes() before being parsed by parse_str() .

(3) Specific examples of parameters in the extracted url are as follows:

<?php
$url='http://www.phpernote.com/ad.php?id=325&action=index&page=3';
$urlarr=parse_url($url);
parse_str($urlarr['query'],$parr);
print_r($urlarr);
print_r($parr);
Copy after login

The printed results are 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

  • Summary of the method of submitting data using curl’s post and getting the method of getting web page data in PHP
  • PHP solution Functions urlencode(), urldecode(), rawurlencode(), rawurldecode() for URL encoding issues
  • PHP detailed explanation of string escape functions (addslashes, stripslashes)
  • PHP to get the complete current page url address function, including parameters
  • PHP Curl batch multi-threaded class to open URLs
  • PHP generates short URL ideas and implementation methods
  • PHP compresses html web page code to reduce network Data transfer volume, clear spaces, tabs, comment marks
  • PHP uses Curl Functions to implement multi-threaded web crawling and file downloading

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/779411.htmlTechArticleThe current url is obtained through php. What should I do if I need to extract the parameters in the url? This process is actually quite simple. It can be completed smoothly using two built-in functions of PHP, namely par...
Related labels:
source:php.cn
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