PHP can also do great things in PHP code decoding detailed

Source: Internet
Author: User
Tags foreach base64 bool html page html tags ord php code urlencode

PHP can also do great things in PHP code decoding detailed

This article mainly introduces PHP can also be able to do great things in PHP coding decoding, this article explains the ASCII codec, URL codec, BASE64 codec, HTML entity codec, binary, octal, decimal, hexadecimal conversion and other content, the need for friends can refer to the

It's written in front.

PHP can also do great things is my summary of PHP syntax features and related functions of the class Library classic usage, it is not necessarily true to achieve 42 of the effectiveness of the dial, but master these methods, you can work and learn some help, I hope we can brainstorm, the "PHP can also do great things" rich more exciting! Reprint please indicate the source (jb51.net)

Second, preface

PHP is a common scripting language, mainly because it is easy to learn, fast to start, almost 50% of the web programs have PHP figure (incomplete statistics). PHP provides rich functions and API interfaces for development, which makes it easy to use its powerful built-in functions and extensions, this is the first of the "PHP can do great things" series, mainly summarizing the knowledge of PHP's decoding and transformation.

Third, PHP codec

1, ASCII codec

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

The PHP basic function has built-in ASCII codec functions, which makes it easy to decode ASCII.

int ord (string $string)//Returns the ASCII code value of the first character in string strings.

string chr (int $ascii)//Returns a single character that corresponds to the specified ASCII.

The code is as follows:

  

$str = ' Welcome to the ';

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 Fu Jing ASCII code n";

echo Getnum ($STR);

echo "n";

echo "ASCII code character n";

Echo GetChar (Getnum ($STR));

/* @OUTPUT

ASCII code for character conversion

87 101 108 99 111 109 101 32 116 111 32 67 104 105-110 97

ASCII code characters

Welcome to

*/

?>

2, URL codec

URL encoding is a format that browsers use to package form input. The browser takes all of the name and the values from the form and sends them to the server as part of the URL or as a separate name/value parameter encoding. For example, when we visit the Web page, there will be a lot of string with%, this is the URL encoding.

URL encoding generally uses UTF-8 encoding format, so it is recommended to use UTF-8 format to pass data. Normal URL encoding can be understood as ASCII code before 16 plus%, no case distinction.

The code is as follows:

String UrlEncode (String $str)//This function makes it easy to encode strings and use them for the request part of a URL, and it also facilitates passing variables to the next page. The space is encoded as +.

String UrlDecode (String $str)//decodes any%xx in the encoded string given, and the plus sign (' + ') is decoded into a space character.

String Rawurlencode (String $str)//The character specified according to RFC 3986 encoding, the space is converted to%20.

String Rawurldecode (String $str)//returns a string in which the sequence of percent semicolon (%) followed by a two-bit hexadecimal number is replaced with a literal character. + is not converted into spaces.

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

The code is as follows:

  

$str _arr = Array (

' Www.jb51.net ',

' http://www.jb51.net/',

' PHP can do big things ',

'!@#$%^&* () _+=-~ ' []{}|;: ' <>,./? '

);

foreach ($str _arr as $key => $value) {

Echo $value, "T->t", UrlEncode ($value), "n";

}

/* @OUTPUT

Www.jb51.net-> www.jb51.net

http://www.jb51.net/-> http%3a%2f%2fwww.jb51.net%2f

PHP can also do great 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 that represents binary data based on 64 printable characters. Because 2 of the 6 times equals 64, each 6 bit is a unit, corresponding to a printable character. Three bytes have 24 bits, corresponding to 4 Base64 units, that is, 3 bytes need to be represented by 4 printable characters. It can be used as a transport encoding for e-mail messages. The characters used include 26 uppercase and lowercase letters, plus 10 digits, and the plus sign "+", Slash "/", 64 characters, and the equal sign "=" used as a suffix. The full base64 definition is visible in RFC 1421 and RFC 2045. The encoded data is slightly longer than the original data, which is 4/3 of the original. In an e-mail message, according to RFC 822, you need to add a carriage return line for every 76 characters. You can estimate 135.1% of the length of the data after the encoding is approximately the original. Https://zh.wikipedia.org/zh/Base64

String Base64_encode (String $data)//encodes data using Base64.

String Base64_decode (String $data [, bool $strict = false])//Decodes Base64 encoded data.

Case: an IMG tag in an HTML page can use Base64 encoding in the SRC attribute to output pictures, which reduces the number of HTTP requests.

Copy code code as follows:

  

$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 the start of the "<" to define HTML tags. If we want the browser to display these characters correctly, we must insert character entities into the HTML source code. Character entities have three parts: A and number "&" and an entity name (or a "#" and an entity number), and a semicolon ";". Http://www.ascii.cl/htmlcodes.htm

String Htmlspecialchars (string $string [, int $flags = Ent_compat | ent_html401 [, String $encoding = "utf-8″[, bool $double = true]]"//HTML entity encoding containing the following HTML special characters

1. ' & ' (ampersand) becomes ' & '

2. ' "' (double quote) becomes '" ' when ent_noquotes are not set.

3. "' (single quote) becomes '" (or) only when 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 is just the opposite of the Htmlspecialchars (). It converts special HTML entities back to normal characters.

There are functions of the same function Htmlentities/html_entity_decode, this pair of functions and even the Chinese characters are HTML entity encoding, and will produce garbled, so it is recommended to use Htmlspecialchars for codec.

Case: Preventing XSS cross-site scripting attacks, requiring HTML entity conversions for user-submitted data:

The code is as follows:

  

$_post[' messages '] = ' test message character ' >

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.