Introduction and use of URL functions in PHP

Source: Internet
Author: User
Tags base64 http request urlencode

  This article mainly introduces PHP processing URL string encoding, decoding and parsing. The following is a detailed illustration of the

Base64_encode-uses MIME base64 to encode the data Base64_encode () returns uses Base64 to encode it. This encoding is designed to enable binary data to be transmitted through a 8-bit transport layer, such as the body of an e-mail message. base64-encoded data consumes about 33% more space than the original data.   Code is as follows: <?php $str = ' This is a encoded string '; vghpcybpcybhbiblbmnvzgvkihn0cmluzw== Echo Base64_encode ($STR);?>      base64_decode-pair using MIME base 64 encoded data for decoding Base64_decode () decoding the Encoded_data, returning the original data, failure returns FALSE. The returned data may be binary.   Code as follows: <?php $str = ' vghpcybpcybhbiblbmnvzgvkihn0cmluzw== '; This is a encoded string echo base64_decode ($STR);?>      get_headers-obtains all headers G sent by server in response to an HTTP request Et_headers () returns an array that contains the headers that the server responds to when an HTTP request is sent. Returns False if it fails and emits an error message at the e_warning level. If you set the optional format parameter to 1, Get_headers () parses the appropriate information and sets the key name of the array.     Code as follows: <?php $phpha 1 = get_headers (' http://www.jb51.net '); $phpha 2 = get_headers (' http://www.jb51.net ', 1); Print_r ($phpha 1); Print_r ($phpha 2);?>     output is as follows:     code as follows: Array (    [0] => HTTP/1.1 OK     [1] => server:nginx/1.2.2     [2] => date:tue, Nov-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-cook ie:saeut=124.127.138.35.1352197078737175; path=/; max-age=311040000) Array (    [0] => http/1.1 OK     [Server] => nginx/1.2.2     [Date] => Tue, Nov 10:17:59 GMT     [content-type] => text/html; Charset=utf-8     [Connection] => close     [x-powered-by] => php/5.3.8     [X-pingbac K] => http://www.jb51.net/xmlrpc.php     [Via] => 10.67.15.21     [Set-cookie] => saeut=124.1 27.138.35.1352197079055460; path=/; max-age=311040000)     Get_meta_tags-Extracts all meta tag content attributes from a file, returning an array can be imagined, some Web sites can easily use this function for the site SEO information extraction.     Code as follows: <?php//tianya php blog http://www.jb51.net $phpha = get_meta_tags (' http://www.jb51.net '); Print_r ($phpha);?>     output is as follows:     code as follows: Array (    [keywords] => tianya blog, PHP blog, PHP technology blog, PH P Learning Blog, PHP development blog     [description] => tianya PHP blog is based on PHP-oriented learning blog, record Phper learning process, pay attention to the latest development of the Internet.     [generator] => WordPress 3.2.1)     http_build_query-request string after generating Url-encode   <?php $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]   This function I use most of the place is to do a variety of APIs, the combination of the requested URL, very convenient. You can also see a prefix for the members of a numeric index within an array.   parse_url-resolves the URL and returns its components this function resolves a URL and returns an associative array containing the various components that appear in the URL. This function is not used to verify the legality of a given URL, but to decompose it into the sections listed below. Incomplete URLs are also accepted, pArse_url () tries to parse it as correctly as possible.     Code as follows: <?php $url = ' Http://tianya:jb51.net@jb51.com/hello.php?id=10#nav '; Print_r (Parse_url ($url));?> Array (    [scheme] => http     [host] => phpha.com   &NBS P [user] => Tianya     [pass] => phphadotcom     [path] =>/hello.php     [query] =&G T id=10     [fragment] => nav)     rawurlencode-encoding URLs according to RFC 1738 rawurldecode-to encoded URL characters String to decode urlencode-encoded URL string urldecode-decode the encoded URL string     code as follows: <?php $url = ' http://www.jb51.net Tianya '; echo UrlEncode ($url); echo ' <br/> '; echo Rawurlencode ($url); echo ' <br/> '; echo UrlDecode ($url); echo ' <br/> '; echo Rawurldecode ($url);?>     output is as follows:     code is as follows: Http%3a%2f%2fwww.jb51.net+tianya http%3a%2f% 2fwww.jb51.net%20tianya     You can see that the difference between UrlEncode and Rawurlencode is that UrlEncode () will encode the space as a plus sign (+), Rawurlencode () The space is encoded as%20 UrlDecode () and RawurldecoDe () is the corresponding decoding function.  

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.