PHP Learning Series (1) -- string processing functions (3), php Functions
11. The crc32 () function calculates the crc32 polynomial of a string. Generate the 32-bit Cyclic Redundancy checksum polynomial of the string parameter. This function can be used to verify data integrity.
Syntax: crc32 (string)
Note: Because PHP integers are signed, many crc32 verification codes return negative integers, so you need to use sprintf () or printf () to obtain the string that represents the crc32 verification code.
Example 1
In this example, the result of crc32 () is output without the "% u" format character (note that the result is the same ):
<?php$str = crc32("Hello world!");echo 'Without %u: '.$str."<br />";echo 'With %u: ';printf("%u",$str);?>
Output:
Without %u: 461707669With %u: 461707669
Example 2
In this example, we will output the crc32 () results without the "% u" format character (note that the results are different ):
<?php$str = crc32("Hello world.");echo 'Without %u: '.$str."<br />";echo 'With %u: ';printf("%u",$str);?>
Output:
Without %u: -1959132156With %u: 2335835140
12. The crypt () function returns a string encrypted with DES, Blowfish, or MD5. In different operating systems, the behavior of this function is different. Some operating systems support more than one algorithm type. During installation, PHP checks what algorithms are available and used.
Syntax: crypt (str, salt)
The salt parameter is optional. It is used to increase the number of characters to be encoded to make the encoding more secure. If the salt parameter is not provided, a random value is generated each time the function is called.
The exact algorithm depends on the salt parameter format and length.
The following are some constants used with the crypt () function. During installation, PHP sets these constants:
- [CRYPT_SALT_LENGTH]
- [CRYPT_STD_DES]
- [CRYPT_EXT_DES]
- [CRYPT_MD5]
- [CRYPT_BLOWFISH]
Note: There is no decryption algorithm. This is a one-way encryption method.
In this example, We will test different algorithms:
<?phpif (CRYPT_STD_DES == 1){echo "Standard DES: ".crypt("hello world")."\n<br />";}else{echo "Standard DES not supported.\n<br />";}if (CRYPT_EXT_DES == 1){echo "Extended DES: ".crypt("hello world")."\n<br />";}else{echo "Extended DES not supported.\n<br />";}if (CRYPT_MD5 == 1){echo "MD5: ".crypt("hello world")."\n<br />";}else{echo "MD5 not supported.\n<br />";}if (CRYPT_BLOWFISH == 1){echo "Blowfish: ".crypt("hello world");}else{echo "Blowfish DES not supported.";}?>
The output is similar (depending on the operating system ):
Standard DES: $1$r35.Y52.$iyiFuvM.zFGsscpU0aZ4e. Extended DES not supported. MD5: $1$BN1.0I2.$8oBI/4mufxK6Tq89M12mk/ Blowfish DES not supported.
13. The explode () function splits strings into arrays.
Syntax: explode (separator, string, limit)
Note: This function returns an array composed of strings. Each element is a substring separated by a separator as a boundary point.
The separator parameter cannot be a null string. If separator is a null string (""), explode () returns FALSE.
If the value of separator cannot be found in string, explode () returns an array containing a single element in string. If the limit parameter is set,
The returned array contains a maximum of limit elements, and the last element contains the rest of the string. If the limit parameter is negative, all elements except the last-limit element are returned.
This feature is added in PHP 5.1.0.
Note: The limit parameter is added to PHP 4.0.1. For historical reasons, although implode () can receive two parameter sequences, explode () cannot.
You must ensureSeparatorParameters inStringBefore the parameter.
Example: In this example, we split the string into an array:
<?php$str = "Hello world. It's a beautiful day.";print_r (explode(" ",$str));?>
Output:
Array([0] => Hello[1] => world.[2] => It's[3] => a[4] => beautiful[5] => day.)
14. The fprintf () function writes formatted strings to the specified output stream (for example, a file or database ).
Returns the length of the Written string.
Syntax
fprintf(stream,format,arg1,arg2,arg++)
Stream -- Optional. Specifies where to write/output strings.
Format -- required. Conversion format.
Arg1 -- required. Specifies the parameter inserted at the first % symbol in the format string.
Arg2 -- Optional. Specifies the parameter inserted at the second % symbol in the format string.
Arg ++ -- Optional. Specifies the parameters inserted to the third, fourth, and other % symbols in the format string.
Description: Parameters
FormatIs the conversion format, starting from the percent sign ("%") to the end of the conversion character. Possible
FormatValue:
- %-Percentage sign returned
- % B-binary number
- % C-Characters Based on ASCII values
- % D-Signed decimal number
- % E-resumable counting (for example, 1.5e + 3)
- % U-Unsigned decimal number
- % F-floating point number (local settings aware)
- % F-floating point number (not local settings aware)
- % O-octal values
- % S-string
- % X-hexadecimal (lowercase letter)
- % X-hexadecimal (uppercase letters)
Parameters such as arg1, arg2, ++ are inserted to the percent sign (%) in the main string. This function is executed step by step. In the first % symbol, insert arg1, at the second % symbol, insert arg2, and so on.
Tips and comments
Note: If the % symbol is greater than the arg parameter, you must use a placeholder. After the placeholder is inserted with the % symbol, it consists of numbers and "\ $. See example 3.
Tip: related functions: printf (), sprintf (), vfprintf (), vprintf (), and vsprintf ().
Example 1
<?php$str = "Hello";$number = 123;$file = fopen("test.txt","w");echo fprintf($file,"%s world. Day number %u",$str,$number)
;?>
Output:
27
The following text is written to "test.txt ":
Hello world. Day number 123
Example 2
<?php$number = 123;$file = fopen("test.txt","w");fprintf($file,"%f",$number);
?>
Output:
123.000000
Example 3
Use placeholders:
<?php$number = 123;$file = fopen("test.txt","w");fprintf($file,"With 2 decimals: %1\$.2f\nWith no decimals: %1\$u",$number)
;?>
The following text is written to "test.txt ":
With 2 decimals: 123.00With no decimals: 123
15. The hebrev () function switches the flow from right to left to the flow from left to right. Only ASCII characters between 224 and 251 are affected.
Syntax: hebrev (string, maxcharline)
Maxcharline -- specifies the maximum number of characters in each line. If possible, hebrev () will avoid breaking words.
Note: hebrev () and hebrevc () can convert the Hebrew logical text to the Hebrew visible text. Hebrew visible text does not require special support for right-to-left characters, which makes it useful for displaying Hebrew text on the web.
How does php handle strings?
Through learning PHP, you can use this advanced language to create a website with high performance. For beginners, the PHP string mbstring is still relatively unfamiliar. Next we will introduce the specific application of the PHP string mbstring.
Multi-language coexistence means Multi-byte. the built-in String Length function strlen in PHP cannot correctly process Chinese strings, and only obtains the number of bytes occupied by strings. For GB2312 Chinese encoding, The strlen value is twice the number of Chinese characters, and for the UTF-8 encoding of Chinese, is 1 ~ 3 times the difference.
Using the PHP string mbstring can better solve this problem. The usage of mb_strlen is similar to that of strlen, except that it has a second optional parameter for specifying character encoding. For example, to get the length of the string $ str for the UTF-8, you can use mb_strlen ($ str, 'utf-8 ′). If the second parameter is omitted, the internal code of PHP is used. The internal encoding can be obtained through the mb_internal_encoding () function. There are two ways to set the internal encoding:
1. Set mbstring. internal_encoding = UTF-8 in php. ini
2. Call mb_internal_encoding ("GBK ")
In addition to the PHP string mbstring, there are many cutting functions, in which mb_substr is used to split characters by words, while mb_strcut is used to split characters by bytes, but no half character is generated. In addition, function cutting has different effects on the length. The cut condition of mb_strcut is smaller than strlen, and that of mb_substr is equal to strlen. See the example below,
<? $ Str = 'I am a long string of Chinese characters -www.jefflei.com'; echo "mb_substr :". mb_substr ($ str, 0, 6, 'utf-8'); echo ""; echo "mb_strcut :". mb_strcut ($ str, 0, 6, 'utf-8');?>
The output is as follows:
Mb_substr: I am a comparison string
Mb_strcut: I am
Note that the PHP string mbstring is not the core function of PHP. before using the function, make sure that the mbstring support is added to the php compilation module:
(1) Use-enable-mbstring during compilation
(2) Modify/usr/local/lib/php. inc
Default_charset = "zh-cn"
Mbstring. language = zh-cn
Mbstring. internal_encoding = zh-cn
The PHP string mbstring class library contains a lot of content. It also includes e-mail processing functions such as mb _ send _ mail.
Is there a direct function for adding a php string to 1? Or code writing
I want to point out that the requirement of the landlord to perform arithmetic operations on strings is distorted,
Even if implemented, the performance efficiency is very low. I personally did not see any practical and theoretical significance.
It can be viewed as Part 2.
Letter section, which is in hexadecimal notation (tens of thousands of hexadecimal notation if the character set includes Chinese), a = 1... z = 26 z + a = 26 + 1 = aa accept 10 hexadecimal addition Input
Digit, in decimal format, 9 + 1 = 10,
In addition, there is still a lack of "a999 + 1 after carry? How to carry ?" Instructions
Multiple possible results
1. Do not carry. A999 + 1 a000,
2. Carry only the decimal part. A999 + 1 a1000
2. Overall carry. A999 + 1 b000
You need to describe this before writing the implementation.