PHP Learning Series (1)--String processing function (3)

Source: Internet
Author: User
Tags crc32 crc32 checksum crypt sprintf

11. The CRC32 () function calculates the crc32 polynomial of a string. A 32-bit cyclic redundancy check code polynomial that generates a string parameter. This function can be used to verify the integrity of the data.

Syntax: CRC32 (String)

Note: Because PHP integers are signed, many CRC32 checksums return negative integers, so you need to use the "%u" format of sprintf () or printf () to get a string that represents the unsigned CRC32 checksum.

Example 1

In this example, we will output the result of CRC32 () (Note the result is the same) in the case of using and not using the "%u" format character:

<? 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 results of the CRC32 () (note the result is not the same) in the case of using and not using the "%u" format character:

<? 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 that is encrypted with DES, Blowfish, or MD5. On different operating systems, this function behaves differently, and some operating systems support more than one type of algorithm. At installation time, PHP checks what algorithms are available and what algorithms are used.

Syntax: Crypt (Str,salt)

The salt parameter is optional. Used to increase the number of characters encoded in a string to make the encoding more secure. If the salt parameter is not provided, one is randomly generated each time the function is called.

The exact algorithm relies on the format and length of the salt parameter.

Here are some constants used with the crypt () function. At installation time, these constants are set by PHP:

    • [Crypt_salt_length]
    • [Crypt_std_des]
    • [Crypt_ext_des]
    • [CRYPT_MD5]
    • [Crypt_blowfish]

Note: The decryption algorithm is not, this is a one-way encryption method

In this example, we will test the different algorithms:

<?PHPif(Crypt_std_des = = 1){Echo"Standard DES:".Crypt("Hello World"). " \N&AMP;LT;BR/&gt; ";}Else{Echo"Standard DES not supported.\n&lt;br/&gt;";}if(Crypt_ext_des = = 1){Echo"Extended DES:".Crypt("Hello World"). " \N&AMP;LT;BR/&gt; ";}Else{Echo"Extended DES not supported.\n&lt;br/&gt;";}if(Crypt_md5 = = 1){Echo"MD5:".Crypt("Hello World"). " \N&AMP;LT;BR/&gt; ";}Else{Echo"MD5 not supported.\n&lt;br/&gt;";}if(Crypt_blowfish = = 1){Echo"Blowfish:".Crypt("Hello World");}Else{Echo"Blowfish DES not supported.";}? >

The output is similar (dependent 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 the string into arrays.
Syntax: Explode (separator,string,limit)
Description: This function returns an array of strings, each of which is a substring separated by separator as a boundary point.
The separator parameter cannot be an empty string. If separator is an empty string (""), Explode () returns FALSE.
If separator contains a value that is not found in a string, explode () returns an array containing a single element in the string. If the limit parameter is set,
The returned array contains a maximum of limit elements, and the last element will contain the remainder of the string. If the limit parameter is a negative number, all elements except the last-limit element are returned.
This feature is new in PHP 5.1.0.
Note: The parameter limit is added in PHP 4.0.1. For historical reasons, although implode () can receive two parameter sequences, explode () does not work.
Separator string parameter before the line.
Example: In this example, we will split the string into arrays:
<? 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 the formatted string to the specified output stream (for example, a file or database).

The function returns the length of the string being written.

Grammar
fprintf (stream,format,arg1,arg2,arg++)

stream--is optional. Specifies where to write/output the string.

format--required. Conversion format.

arg1--required. Specifies the parameter that is inserted into the first% symbol in the format string.

arg2--is optional. Specifies the parameter that is inserted into the second% symbol in the format string.

arg++--is optional. Specifies the parameter that is inserted into the format string third to fourth, and so on, in the% symbol.

Description: Parameters formatis the converted format, starting with the percent sign ("%") to the end of the converted character. The following possible formatValue:
    • Percent-return percentage symbol
    • %b-Binary number
    • %c-Characters in accordance with ASCII values
    • %d-Signed decimal number
    • %e-Sustainable counting method (e.g. 1.5e+3)
    • %u-Unsigned decimal number
    • %f-Floating point (Local settings Aware)
    • %F-Floating point number (not local settings aware)
    • %o-Eight binary number
    • %s-String
    • %x-16 binary number (lowercase letters)
    • %x-16 decimal digits (uppercase letters)

Arg1, arg2, + + etc parameters will be inserted into the main string percent percent (%) Symbol. This function is executed step-by. In the first% symbol, insert arg1, insert arg2 at the second% symbol, and so on.

Hints and Notes

Note: If the% symbol is more than the arg parameter, you must use a placeholder. After the placeholder is inserted in the% symbol, it consists of a number and a "\$". See Example 3.

Tip: Related functions: printf (), sprintf (), vfprintf (), vprintf (), and vsprintf ().

Example Example 1
fprintf($file,"%s world. Day number %u",$str,$number);? >

Output:

27

The following text will be 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 will be written to "test.txt":

With 2 Decimals:123.00with no decimals:123

15. The Hebrev () function converts a right-to-left stream of Hebrew text to a left-to-right stream. Only ASCII characters between 224 and 251, as well as punctuation marks, are affected.

Syntax: Hebrev (string,maxcharline)

maxcharline--Specifies the maximum number of characters per line. If possible, Hebrev () will avoid breaking the word.

Description: Hebrev () and HEBREVC () can convert Hebrew logical text to Hebrew visible text. Hebrew visible text does not require special right-to-left character support, which makes it useful for displaying Hebrew text on the web.

PHP Learning Series (1)--String processing function (3)

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.