Summary of url processing functions in php

Source: Internet
Author: User
Tags compact mixed rfc urlencode alphanumeric characters

Http_build_query

(PHP 5) http_build_query-request string generated after URL-encode

The code is as follows: Copy code

<? Php
$ Data = array ('foo' => 'bar ',
'Baz' => 'boom ',
'Cow' => 'milk ',
'Php' => 'HyperText processor '); echo http_build_query ($ data); // foo = bar & baz = boom & cow = milk & php = hypertext + processor
?>


Compact

Array compact (mixed $ varname [, mixed $...])
(PHP 4, PHP 5) compact-create an array, including variable names and their values

The code is as follows: Copy code

<? Php
$ City = "San Francisco ";
$ State = "CA ";
$ Event = "SIGGRAPH ";
$ Result = compact ("city", "state", "event ");
// Array ('city' => '"San Francisco"', 'state' => 'CA', 'event' => "SIGGRAPH ")
?>
-------------

$ Qs = compact ('Vince ', 'city', 'name ');
Foreach ($ qs as $ key => $ value ){
If (! $ Value ){
Unset ($ qs [$ key]);
    }
}
$ Url = 'something/search /? '. Http_build_query ($ qs );


Urldecode, urlencode
The following describes the introduction and examples.
Parse_url ($ str url );
Convert a url into an array

The code is as follows: Copy code

*/
Print_r (parse_url ("www.111cn.net"); // Parse the url and output the returned array

/*

Returns a string in special url format to a normal string.

Syntax: string rawurldecode (string str );

Return value: string

Function type: encoding
 
Description


This function decodes strings. Parses the string from the special format of the url into a normal string. For detailed encoding and decoding information and specification files, refer to rfc 1738.

The code is as follows: Copy code

*/

Echo rawurldecode ('foo % 20bar % 40baz'); // output foo bar @ baz
/*

String rawurlencode (string str)


Returns a string. All non-alphanumeric characters except-_. In this string will be replaced with a semicolon (%) followed by two hexadecimal numbers. This encoding is described in rfc 1738 to protect the original characters from being interpreted as special url delimiters and protect the url format to prevent them from being transmitted to media (like some email systems) use character conversion. For example, if you want to include a password in an ftp url:

 

The code is as follows: Copy code

*/
$ Str = "http://www.111cn.net"; // defines a string
$ Result = rawurlencode ($ str); // Encode the specified string
Echo $ result;

/*
Urldecode ()
Url decoding
* /// Output result

$ Str = "http % 3a % 2f % 2fwww.111cn.net ";
$ Result = urldecode ($ str );
Echo $ result;

/*
Urlencode ()
Url encoding

*/

$ Str = "http://www.111cn.net"; // defines a string
$ Result = urlencode ($ str); // Encode the specified string
Echo $ result; // output result

Related Article

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.