Introduction: Processing URL strings: encoding, decoding and parsing. Tianya provides detailed examples below.
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.
$str = 'This is an encoded string';
// VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw==
echo base64_encode($str);
?>
base64_decode — Use MIME base64 Decode the encoded data
base64_decode() decodes the encoded_data and returns the original data. If it fails, it returns FALSE. The data returned may be binary.
$str = 'VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw==';
// This is an encoded string
echo base64_decode($str);
?>
get_headers — Get a server response All headers sent by the 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.
//Tianya PHP Blog http://blog.phpha.com
$phpha1 = get_headers('http://blog.phpha.com');
$phpha2 = get_headers(' http://blog.phpha.com', 1);
print_r($phpha1);
print_r($phpha2);
?>
//The output 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] => /blog.phpha.com/xmlrpc.php
[7] => Via: 10.67.15.26
[8] => Set-Cookie: saeut=124.127.138.35.1352197078737175; path=/; max-age=311040000
)
//Tianya PHP Blog http://blog.phpha.com
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://blog.phpha.com/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.
【Tianya Note】As you can imagine, some websites can easily use this function to extract website SEO information.
//Tianya PHP Blog http://blog.phpha.com
$phpha = get_meta_tags('http://blog.phpha.com');
print_r($phpha);
?>
//The output is as follows:
Array
(
[keywords] => Tianya Blog, PHP Blog, PHP Technology Blog, PHP Learning Blog, PHP Development Blog
[description] => Tianya PHP Blog is based on A PHP-based learning blog that records the learning process of PHPER and pays attention to the latest development trends of the Internet.
[generator] => WordPress 3.2.1
)
http_build_query — Generate the request string after URL-encoding
$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, 'phpha_');
?>
[Tianya Note] This function is currently the one I use the most The point is that when making various APIs, it is very convenient to combine the requested URLs.
In addition, you can see that for members with numerical indexes in the array, you can also specify a prefix.
parse_url — Parse a URL and return 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 a given URL, only 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.
$url = 'http://tianya:phphadotcom@phpha.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 — 按照 RFC 1738 对 URL 进行编码
rawurldecode — 对已编码的 URL 字符串进行解码
urlencode — 编码 URL 字符串
urldecode — 解码已编码的 URL 字符串
//Tianya PHP blog http://blog.phpha.com
$url = 'http://blog.phpha.com tianya';
echo urlencode($url);
echo '
';
echo rawurlencode($url);
echo '
';
echo urldecode($url);
echo '
';
echo rawurldecode ($url);
?>
The output is as follows:
http%3A%2F%2Fblog.phpha.com+tianya
http%3A%2F%2Fblog.phpha.com%20tianya
http://blog.phpha. com tianya
http://blog.phpha.com tianya
As you can see, the difference between urlencode and rawurlencode is:
urlencode() will encode spaces into plus signs (+), and rawurlencode() will encode spaces into The encoding is %20
urldecode() and rawurldecode() are the corresponding decoding functions.
The above is excerpted from the PHP manual [6] – URL function. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!