This article describes how to process, decode, and parse url strings in PHP. The following is a detailed example of base64_encode-encoding data using MIME base64.
Base64_encode () returns uses base64 to encode data. This encoding is designed to enable binary data to be transmitted through a non-pure 8-bit transport layer, such as the subject of an email.
Base64-encoded data takes up about 33% more space than raw data.
The code is as follows:
$ Str = 'This is an encoded string ';
// VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw =
Echo base64_encode ($ str );
?>
Base64_decode-decodes data encoded with MIME base64
Base64_decode () decodes encoded_data and returns the original data. if it fails, FALSE is returned. The returned data may be binary.
The code is as follows:
$ Str = 'vghpcybpcybhbiblbmnvzgvkihn0cmluzw = ';
// This is an encoded string
Echo base64_decode ($ str );
?>
Get_headers-get all headers sent by the server in response to an HTTP request
Get_headers () returns an array containing the header sent by the server to respond to an HTTP request. If it fails, FALSE is returned and an E_WARNING-level error message is sent.
If you set the optional format parameter to 1, get_headers () will parse the corresponding information and set the key name of the array.
The code is as follows:
$ Phpha1 = get_headers ('http: // www.bitsCN.com ');
$ Phpha2 = get_headers ('http: // www.bitsCN.com ', 1 );
Print_r ($ phpha1 );
Print_r ($ phpha2 );
?>
The output is as follows:
The 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] => X-Pingback: http://www.bitsCN.com/xmlrpc.php
[7] => Via: 10.67.15.26
[8] => Set-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; charsets = UTF-8
[Connection] => close
[X-Powered-By] => PHP/5.3.8
[X-Pingback] => http://www.bitsCN.com/xmlrpc.php
[Via] => 10.67.15.21
[Set-Cookie] = & gt; 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.
The code is as follows:
// Tianya PHP blog http://www.bitsCN.com
$ Phpha = get_meta_tags ('http: // www.bitsCN.com ');
Print_r ($ phpha );
?>
The output is as follows:
The 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 PHP-based learning blog that records the learning history of PHPER and keeps an eye on the latest developments in the Internet.
[Generator] => WordPress 3.2.1
)
Http_build_query-request string generated after URL-encode
$ 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]
Currently, the most commonly used function is to combine the request url for various APIs, which is very convenient.
You can also specify the prefix for the number index members in the array.
Parse_url-parses the URL and returns its components
This function parses a URL and returns an associated array, which contains various components in the URL. This function is not used to verify the validity of a given URL, but is used to separate it into the parts listed below. Incomplete URLs are also accepted, and parse_url () tries to parse them correctly as much as possible.
The code is as follows:
$ Url = 'http: // tianya: bitsCN.com@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 the URL according to RFC 1738
Rawurldecode-decode the encoded URL string
Urlencode-encode a URL string
Urldecode-decode the encoded URL string
The code is as follows:
$ Url = 'http: // www.bitsCN.com tianya ';
Echo urlencode ($ url );
Echo'
';
Echo rawurlencode ($ url );
Echo'
';
Echo urldecode ($ url );
Echo'
';
Echo rawurldecode ($ url );
?>
The output is as follows:
The code is as follows:
Http % 3A % 2F % 2Fwww.bitsCN.com + tianya
Http % 3A % 2F % 2Fwww.bitsCN.com % 20 tianya
We can see that the difference between urlencode and rawurlencode is:
Urlencode () encodes the space into the plus sign (+), and rawurlencode () encodes the space into % 20
Urldecode () and rawurldecode () are the corresponding decoding functions.