Introduction and usage examples of url function in php_PHP tutorial

WBOY
Release: 2016-07-13 10:37:30
Original
734 people have browsed it

base64_encode — Encode data using MIME base64
base64_encode() returns Encode data using base64. This encoding is designed to enable binary data to be transmitted over a transport layer that is not pure 8-bit, such as the body of an email.
Base64-encoded data takes up about 33% more space than the original data.

Copy code The code is as follows:

$str = 'This is an encoded string';
// VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw==
echo base64_encode($str);
?>

base64_decode — Decode data encoded using MIME base64
base64_decode() decodes encoded_data, returning the original data, or FALSE on failure. The data returned may be binary.

Copy code The code is as follows:

$str = 'VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw==';
// This is an encoded string
echo base64_decode($str);
?>

get_headers — Get all the headers sent by the server in response to an HTTP request
get_headers() returns an array containing the headers sent by the server in response to an HTTP request. If it fails, it returns FALSE and issues an E_WARNING level error message.
If the optional format parameter is set to 1, get_headers() will parse the corresponding information and set the key name of the array.

Copy code The code is as follows:

$phpha1 = get_headers('http://www .jb51.net');
$phpha2 = get_headers('http://www.jb51.net', 1);
print_r($phpha1);
print_r($phpha2);
?>

The output is as follows:

Copy codeThe code is as follows:

Array
(
[0] = > HTTP/1.1 200 OK
[1] => Server: nginx/1.2.2
[2] => Date: Tue, 06 Nov 2012 10:17:59 GMT
[3 ] => Content-Type: text/html; charset=UTF-8
[4] => Connection: close
[5] => X-Powered-By: PHP/5.3.8
[6] => Cookie: saeut=124.127.138.35.1352197078737175; path=/; max-age=311040000
)
Array
(
[0] => HTTP/1.1 200 OK
[Server ] => nginx/1.2.2
[Date] => Tue, 06 Nov 2012 10:17:59 GMT
[Content-Type] => text/html; charset=UTF-8
[Connection] => close
[X-Powered-By] => PHP/5.3.8
[X-Pingback] => http://www.jb51.net/xmlrpc. php
[Via] => 10.67.15.21
[Set-Cookie] => saeut=124.127.138.35.1352197079055460; path=/; max-age=311040000
)


get_meta_tags — Extract all meta tag content attributes from a file and return an array As you can imagine, some websites can easily use this function to extract website SEO information.


Copy code The code is as follows:
//Tianya PHP Blog http://www. jb51.net
$phpha = get_meta_tags('http://www.jb51.net');
print_r($phpha);
?>


The output is as follows:


Copy the codeThe code is as follows:
Array
(
[keywords] = > Tianya Blog, PHP Blog, PHP Technology Blog, PHP Learning Blog, PHP Development Blog
[description] => Tianya PHP Blog is a learning blog focusing on PHP. It records the learning process of PHPER and pays attention to the latest development of the Internet. Dynamic.
[generator] => WordPress 3.2.1
)

http_build_query — Generate URL-encoded request string

$url = array('c'=>'blog', 'a'=>'show', 'id'=>10, 'hello', 'world' );
// c=blog&a=show&id=10&0=hello&1=world
echo http_build_query($url);
// c=blog&a=show&id=10&phpha_0=hello&phpha_1=world
echo http_build_query( $url, 'jb51_');
?>
[/code]

The place where I use this function the most is to combine the requested URLs when making various APIs, which is very convenient.
In addition, you can see that for members with numerical indexes in the array, you can also specify a prefix.

parse_url — Parses a URL and returns its components
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, only to break it into the parts listed below. Incomplete URLs are also accepted and parse_url() will try to parse them as correctly as possible.

Copy code The code is as follows:

$url = 'http://tianya:jb51 .net@jb51.com/hello.php?id=10#nav';
print_r(parse_url($url));
?>
Array
(
[scheme] => http
[host] => phpha.com
[user] => tianya
[pass] => phphadotcom
[path] => /hello.php
[query] => id=10
[fragment] => nav
)

rawurlencode — Encode a URL per RFC 1738
rawurldecode — Decode an encoded URL string
urlencode — Encode a URL string
urldecode — Decode an encoded URL string

Copy code The code is as follows:

$url = 'http://www.jb51 .net tianya';
echo urlencode($url);
echo '
';
echo rawurlencode($url);
echo '
' ;
echo urldecode($url);
echo '
';
echo rawurldecode($url);
?>

The output is as follows:

Copy the codeThe code is as follows:

http%3A%2F%2Fwww.jb51.net+ tianya
http%3A%2F%2Fwww.jb51.net%20tianya

As you can see, the difference between urlencode and rawurlencode is:
urlencode() will encode spaces as plus signs (+), while rawurlencode() will encode spaces as %20
urldecode() and rawurldecode( ) is the corresponding decoding function.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/735241.htmlTechArticlebase64_encode — Encode data using MIME base64 base64_encode() returns Encode data using base64. This encoding is designed to allow binary data to pass through impure...
Related labels:
php
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!