


PHP can also do great things - Detailed explanation of encoding and decoding in PHP_PHP Tutorial
Detailed explanation of encoding and decoding in PHP can also do great things with PHP
PHP can also do great things with detailed explanation of encoding and decoding in PHP
This article mainly introduces the detailed explanation of encoding and decoding in PHP that PHP can do great things. This article explains ASCII encoding and decoding, URL encoding and decoding, Base64 encoding and decoding, HTML entity encoding and decoding, binary, octal, decimal, and hexadecimal For content such as system conversion and mutual conversion, friends in need can refer to it
Write 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 Big Things" more exciting! Please indicate the source for 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 have 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.
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
Character 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.
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. 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.
The code is as follows:
$str_arr = array(
'www.jb51.net',
'http://www.jb51.net/',
'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.jb51.net/ -> 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 the 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 a function htmlentities/html_entity_decode with the same function. This pair of functions encodes HTML entities even for Chinese characters, and will produce garbled characters, so it is recommended to use htmlspecialchars for encoding and decoding.
Case: To prevent XSS cross-site scripting attacks, the data submitted by the user needs to be converted into HTML entities:
The code is as follows:
$_POST['message'] = 'Test message character'">

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
