Introduction and use of url functions in php _ PHP Tutorial

Source: Internet
Author: User
The url function in php is described and used as an example. Base64_encode-use MIMEbase64 to encode base64_encode () returns with base64. This encoding is designed to enable binary data to be encoded using a non-pure base64_encode-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.jb51.net ');
$ Phpha2 = get_headers ('http: // www.jb51.net', 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.jb51.net/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.jb51.net/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.jb51.net
$ Phpha = get_meta_tags ('http: // www.jb51.net ');
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: 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 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.jb51.net 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.jb51.net + tianya
Http % 3A % 2F % 2Fwww.jb51.net % 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.

Http://www.bkjia.com/PHPjc/735241.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/735241.htmlTechArticlebase64_encode-use MIME base64 to encode base64_encode () returns use base64 to encode data. This encoding is designed so that binary data can be passed through non-pure...

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.