Home > Backend Development > PHP Tutorial > PHP can also do great things - detailed explanation of encoding and decoding in PHP, detailed explanation of php encoding and decoding_PHP tutorial

PHP can also do great things - detailed explanation of encoding and decoding in PHP, detailed explanation of php encoding and decoding_PHP tutorial

WBOY
Release: 2016-07-13 09:56:36
Original
835 people have browsed it

PHP can also do great things. Detailed explanation of encoding and decoding in PHP. Detailed explanation of php encoding and decoding

is written in front

PHP can also do great things. This is the classic usage of PHP syntax features and related function libraries that I have summarized. It may not really be able to achieve the effect of making a big difference, but mastering these methods can be of some help in your work and study. , I hope everyone can brainstorm and make "PHP Can Do Great Things" more exciting! Please indicate the source when reprinting (jb51.net)

2. Foreword

PHP is a common scripting language, mainly because it is easy to learn and quick to use. Almost 50% of web programs include PHP (incomplete statistics). PHP provides a wealth of functions and API interfaces for development, which allows us to use its powerful built-in functions and extensions very conveniently. This article is the first article in the series "PHP Can Do Big Things", which mainly summarizes the advantages of PHP in encoding, decoding, Knowledge of base conversion.

3. PHP encoding and decoding

1. ASCII encoding and decoding

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

PHP basic functions have built-in ASCII encoding and decoding functions, which allows us to easily perform ASCII encoding and decoding.

int ord ( string $string ) //Returns the ASCII code value of the first character of string string.
string chr ( int $ascii ) //Returns a single character corresponding to ascii specified.
Copy code The code is as follows:
$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 to ASCII code n";
echo getNum($str);
echo "n";
echo "ASCII character n";
echo getChar(getNum($str));
/* @OUTPUT
Convert characters to ASCII code
87 101 108 99 111 109 101 32 116 111 32 67 104 105 110 97
ASCII character
Welcome to China
*/

?>

2. URL encoding and decoding

URL encoding is a format used by browsers to package form inputs. The browser retrieves all names and values ​​from the form and sends them to the server as part of the URL or separately as name/value parameter encoding. For example, when we visit a web page, there will be many strings with %, which is URL encoding.

URL encoding generally uses UTF-8 encoding format, so it is recommended to use UTF-8 format to transfer data. The URL encoding in the normal sense can be understood as the hexadecimal number of the ASCII code plus % before it, and there is no case distinction.
Copy code The code is as follows:
string urlencode(string $str) //This function facilitates encoding a string and using it in the request part of the URL, and it also facilitates passing variables to the next page. Spaces are encoded as .
string urldecode(string $str) //Decode any %XX in the given encoded string, the plus sign (' ') is decoded into a space character.
string rawurlencode (string $str) //Encode the specified characters according to RFC 3986, and convert spaces into .
string rawurldecode (string $str) //Returns a string. The sequence of percent signs (%) followed by two hexadecimal digits in this string will be replaced with literal characters. Not converted to spaces.

The two sets of functions have the same usage, except for the conversion processing of and spaces: rawurlencode converts spaces into and does not convert into spaces; urlencode is different.

Copy code The code is as follows:
$str_arr = array(
'www.jb51.net',
'http://www.bkjia.com/',
‘PHP can also 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://www.jb51.net/
PHP can also do big things -> PHP can also do big things
!@#$%^&*()_ =-~`[]{}|;:'"<>,./? -> !@#$%^&*()_+=-~ `[]{}|;:'",./?
*/
?>

3. Base64 encoding and decoding

Base64 is a representation method for binary data based on 64 printable characters. Since 2 to the 6th power is equal to 64, every 6 bits 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 transfer encoding for email. The characters used include 26 uppercase and lowercase letters, plus 10 numbers, plus sign " ", slash "/", a total of 64 characters, and the equal sign "=" is used as a suffix. The complete base64 definition can be found in RFC 1421 and RFC 2045. The encoded data is slightly longer than the original data, 4/3 of the original length. In emails, according to RFC 822, a carriage return and line feed must be added for every 76 characters. It can be estimated that the encoded data length is approximately 135.1% of the original length. https://zh.wikipedia.org/zh/Base64

string base64_encode(string $data) //Use base64 to encode data.
string base64_decode (string $data [, bool $strict = false ]) //Decode base64 encoded data.

Case: The img tag in the HTML page can use base64 encoding in the src attribute to output images, which can reduce the number of HTTP requests.
Copy code The code is as follows:
$string = file_get_content('3mc2.png');
echo '';
/* @OUTPUT
UEhQ5Lmf6IO95Yqe5aSn5LqL
*/
?>

4. HTML entity encoding and decoding

Some characters are reserved in HTML and have special meanings. For example, the less than sign "<" is used to define the beginning of HTML tags. If we want the browser to display these characters correctly, we must insert character entities in the HTML source code. Character entities have three parts: an ampersand "&" 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_encode = true ]]] ) //Convert HTML to the following HTML special characters Entity encoding
1.'&' (ampersand) becomes ‘&'
2.'"' (double quote) becomes ‘"' when ENT_NOQUOTES is 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 of this function is exactly the opposite of htmlspecialchars(). It converts special HTML entities back to normal characters.
There is also the function htmlentities/html_entity_decode with the same function. This pair of functions even encodes Chinese characters into HTML entities, and will produce garbled characters, so it is recommended to use htmlspecialchars for encoding and decoding.

Case: Preventing XSS cross-site scripting attacks requires HTML entity conversion of user-submitted data:

Copy code The code is as follows:
$_POST['message'] = 'Test message character'">

Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template