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.
Copy Code code as follows:
<?php
$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";
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.
Copy Code code 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.
Copy Code code as follows:
<?php
$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:
<?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 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:
Copy Code code as follows:
<?php
$_post[' messages '] = ' test message character \ ' ' ><script src=http://www.jb51.net/hook.js> ';
if (Empty ($_post[' message ')) {
Exit (' message is NULL ');
}
$message = Htmlspecialchars (Trim ($_post[' message '));
Echo $message;
/* @OUTPUT
Test message character ' "><script src=http://www.jb51.net/hook.js>
*/
?>
5, binary, octal, decimal, Hex mutual conversion
Transitions between systems Here's nothing to say, anyway, just remember how many into the system is how many into a, such as 10 into the system is 9 of the next is 10, binary, octal, hexadecimal and so on.
String Bin2Hex (String $str)//Returns the ASCII string, which is the hexadecimal representation of the parameter str. The conversion uses the byte method, the high four bit byte first.
String Hex2bin (String $data)//convert hexadecimal string to binary string.
Number Bindec (String $binary _string)//returns the decimal value of the binary as represented by the binary_string parameter.
String decbin (int $number)//Returns a string containing a binary representation of a given number parameter. The maximum number that can be converted is 4294967295 in decimal, and the result is a string of 32 1.
Number Octdec (String $octal _string)//returns the decimal equivalent of the octal count as represented by the octal_string parameter.
String decoct (int $number)//Returns a string containing a octal representation with the given number parameter. The maximum number that can be converted is 4294967295 in decimal, and the result is "37777777777".
String Base_convert (string $number, int $frombase, int $tobase)//arbitrary transform, returns a string containing the number to represent the tobase. The number itself is specified by Frombase. Frombase and Tobase are only between 2 and 36 (including 2 and 36). Numbers above decimal are represented by a letter A-Z, for example a denotes 10,b 11 and Z represents 35.
6, GBK, UTF-8 character encoding conversion
In the process of writing code, often encountered coding problems caused by garbled. In fact, solve the coding problem is very simple, as long as the use of a coding can, in general, the adoption of universal code--UTF-8 is the best choice.
The coding here is the encoding of text encoding and file storage, of course, it is necessary to mention the difference of the coding of the system:
End of system encoded character
Windows GBK \ r \ n
*nix UTF-8 \ n
So pay special attention when dealing with special characters.
Common coding has GBK, UTF-8 and so on, the function uses generally uses two kinds:
String mb_convert_encoding (String $str, String $to _encoding [, mixed $from _encoding = mb_internal_encoding ()])//Will Str The character encoding of the ING type Str is converted from an optional from_encoding to a to_encoding.
String Iconv (String $in _charset, String $out _charset, String $str)//encode string str from In_charset conversion to Out_charset.
Case: Windows system, set up a Wamp server, the following script saved as a UTF-8 encoded PHP file, you can see through the browser without garbled PHP directory files; if not with mb_convert_encoding transcoding, Will directly result in garbled output (Windows as server).
Copy Code code as follows:
<?php
function Getdir ($dir) {
static $string = ';
if (Is_file ($dir)) {
$string. = $dir;
}else{
$oDir = @opendir ($dir);
while ($fileName = Readdir ($oDir)) {
if ($fileName!= '. ' && $fileName!= ' ... ') {
if (Is_file ($dir. '/'. $fileName)) {
$string. = $fileName. " \ n ";
}elseif (Is_dir ($dir. '/'. $fileName)) {
$string. = $dir. '/' $fileName. ' /'." \ n ";
Getdir ($dir. '/'. $fileName);
}
}
}
}
return $string;
}
Echo mb_convert_encoding (Getdir (' php '), ' utf8 ', ' gbk ');
?>
Iv. Summary
Coding is the basis of data processing, so it is very important to develop PHP in the process of programming. For the processing of PHP, the application in the programming also need to master the number, especially some similar functions to distinguish. Reprint please indicate the source (jb51.net)