PHP can also do great things in PHP code decoding in detail, PHP encoding and decoding detailed _php tutorial

Source: Internet
Author: User
Tags printable characters rfc types of functions urlencode

PHP can also do great things in PHP code decoding in detail, PHP encoding and decoding detailed


Write in front

PHP can also do big things is my summary of PHP syntax features and related functions of the classic use of library, and is not really able to achieve the effect of 42 dial, but master these methods, you can work and study some help, I hope you can brainstorm, will "PHP can do big things" rich more wonderful! Reprint please specify the source (jb51.net)

Second, preface

PHP is a common scripting language, mainly because it is easy to learn, quick to get started, almost 50% of the web programs have PHP figure (not fully counted). PHP for the development of this provides a rich function and API interface, which makes it very convenient to use its powerful built-in functions and extensions, this article is the "PHP can do great things" series of the first, the main summary of PHP in the decoding, conversion knowledge.

Third, PHP codec

1. ASCII codec

ASCII (pronunciation: English pronunciation:/ˈæski/ass-kee,american Standard code for Information Interchange, U.S. Information Interchange standards codes) is a set of computer coding systems based on the Latin alphabet. It is primarily used to display modern English, while its extended version Eascii can partially support other Western European languages and is equivalent to ISO/IEC 646. Since the World Wide Web made ASCII widely universal, it was gradually replaced by Unicode until December 2007. Https://zh.wikipedia.org/zh/ASCII

The PHP basic functions have built-in ASCII codec functions, which makes it easy for ASCII codecs to be encoded.

int ord (string $string)//Returns the ASCII code value of the first character of a string.
string chr (int $ascii)//Returns a single character relative to the ASCII specified.
Copy the Code code as follows:
<?php
$str = ' Welcome to China ';
function Getnum ($string) {
$needle = 0;
$num = ";
while (Isset ($string [$needle])) {
$num. = $num ==0? ': ';
$num. = Ord ($string [$needle]);
$needle + +;
}
return $num;
}
function GetChar ($num) {
$num _arr = Explode (", $num);
$string = ";
foreach ($num _arr as $value) {
$string. = Chr ($value);
}
return $string;
}
echo "character representable ASCII code \ n";
echo Getnum ($STR);
echo "\ n";
echo "ASCII code character \ n";
Echo GetChar (Getnum ($STR));
/* @OUTPUT
ASCII code for character transfer
87 101 108 99 111 109 101 32 116 111 32 67 104 105 110 97
ASCII code characters
Welcome to China
*/

?>

2. URL encoding and decoding

URL encoding is a format that a browser uses to package form input. The browser obtains all the name and its values from the form, and sends them to the server as part of the URL or separated from the Name/value parameter encoding. For example, when we visit the Web page, there will be a lot of strings with a%, which is the URL encoding.

URL encoding is generally used in UTF-8 encoding format, so it is recommended to use UTF-8 format to pass data. The normal meaning of the URL code can be understood as ASCII code of 16 binary before plus%, no case sensitivity.
Copy the Code code as follows:
String UrlEncode (String $str)//This function makes it easy to encode a string and use it for the request part of a URL, and it also facilitates passing a variable to the next page. The space is encoded as +.
String UrlDecode (String $str)//decodes any%XX in the encoded string given, plus (' + ') is decoded into a space character.
String Rawurlencode (String $str)//characters specified according to RFC 3986 encoding, spaces converted to%20.
String Rawurldecode (String $str)//returns a string in which the percent (%) of the string followed by a sequence of two-bit hexadecimal digits is replaced with the literal character. + is not converted into spaces.

Two sets of function usages, except for the conversion processing of + and space: Rawurlencode the space to% 20, not the + to a space; UrlEncode is not the same.

Copy the Code code as follows:
<?php
$str _arr = Array (
' Www.jb51.net ',
' http://www.bkjia.com/',
' PHP can do great things ',
'!@#$%^&* () _+=-~ ' []{}|\\;:\ '] <>,./? '
);
foreach ($str _arr as $key = + $value) {
Echo $value, "\t->\t", UrlEncode ($value), "\ n";
}
/* @OUTPUT
Www.jb51.net-Www.jb51.net
http://www.bkjia.com/-http%3a%2f%2fwww.jb51.net%2f
PHP can do big things--php%e4%b9%9f%e8%83%bd%e5%b9%b2%e5%a4%a7%e4%ba%8b
!@#$%^&* () _+=-~ ' []{}|\;: ' <>,./? %21%40%23%24%25%5e%26%2a%28%29_%2b%3d-%7e%60%5b%5d%7b%7d%7c%5c%3b%3a%27%22%3c%3e%2c.%2f%3f
*/
?>

3, Base64 codec

Base64 is a representation of binary data based on 64 printable characters. Since 2 of the 6 is equal to 64, every 6 bits is a unit that corresponds to a printable character. Three bytes have 24 bits, which correspond to 4 Base64 units, 3 bytes need to be represented by 4 printable characters. It can be used as the transmission encoding for e-mail. The characters used include 26 uppercase and lowercase letters, plus 10 digits, and a plus sign "+", a slash "/", a total of 64 characters, and an equal sign "=" used for suffix purposes. The full base64 definition is visible in RFC 1421 and RFC 2045. The encoded data is slightly longer than the original data for the original 4/3. In the e-mail message, according to RFC 822, each 76 characters, plus a carriage return to wrap. You can estimate the length of the data after encoding is approximately 135.1% of the original. Https://zh.wikipedia.org/zh/Base64

String Base64_encode (String $data)//uses Base64 to encode data.
String Base64_decode (String $data [, bool $strict = false])//Decodes the Base64 encoded data.

Case: an IMG tag in an HTML page can output a picture using the Base64 encoding in the SRC attribute, which can reduce the number of HTTP requests.
Copy the Code code as follows:
<?php
$string = file_get_content (' 3mc2.png ');
Echo ';
/* @OUTPUT
Uehq5lmf6io95yqe5asn5lql
*/
?>

4. HTML Entity Codec

Some characters are reserved in HTML and have special meanings, such as less than "<", which defines the beginning of the HTML tag. If we want the browser to display these characters correctly, we must insert the character entities in the HTML source. The character entity has three parts: one and number "&" and one entity name (or one "#" and one entity number), and one semicolon ";". Http://www.ascii.cl/htmlcodes.htm

String Htmlspecialchars (string $string [, int $flags = Ent_compat | ent_html401 [, String $encoding = "utf-8″[, bool $double _encode = True]])//HTML entity encoding for HTML special characters including the following
1. ' & ' (ampersand) becomes ' & '
2. ' ' "' (double quote) becomes '" When Ent_noquotes was not set.
3. "'" (single quote) becomes ' (or ') if Ent_quotes is set.
4. ' < ' (less than) becomes ' < '
5. ' > ' (greater than) becomes ' > '

String Htmlspecialchars_decode (string $string [, int $flags = Ent_compat | ENT_HTML401])//The function and Htmlspecialchars () are just the opposite. It converts special HTML entities back to normal characters.
There are functions of the same function Htmlentities/html_entity_decode, this function even the Chinese characters are HTML entity encoding, and will produce garbled, it is recommended to use Htmlspecialchars to encode and decode.

Case: Preventing XSS Cross-site scripting attacks requires HTML entity conversions for user-submitted data:

Copy CodeThe code is as follows:
<?php
$_post[' message ' = ' test message character \ ' ' >

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.