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

Source: Internet
Author: User
Tags first string locale setting sha1 sprintf

31, the Ord () function returns the ASCII value of the first character of a string.

Syntax: Ord (String)

Example
<? PHP Echo Ord ("H"); Echo Ord ("Hello"); >

Output:

104104

32. The Parse_str () function parses the query string into a variable.

Syntax: Parse_str (String,array)

array--is optional. Specifies the array name for the stored variable. This parameter indicates that the variable is stored in the array.

Description: If the array parameter is not set, the variable set by the function overrides the variable with the same name. The MAGIC_QUOTES_GPC setting in php.ini affects the output of the function. If enabled, the variable is converted by addslashes () before Parse_str () is resolved. Example Example 1
<? PHP Parse_str ("Id=23&name=john%20adams"); Echo $id. " <br/> "; Echo $name ;? >

Output:

23John Adams
Example 2
<? PHP Parse_str ("Id=23&name=john%20adams",$myArray); Print_r ($myArray);? >

Output:

Array ([id] = 23[name] = John Adams)

33. Print () function outputs one or more strings

Description: The print () function is not actually a function, so you do not have to use parentheses on it. The print () function is slightly slower than echo () because it needs to return 1.

34. The printf () function outputs a formatted string.

printf (format,arg1,arg2,arg++)

format--required. Specifies the string and how to format the variable.

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

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

arg++--is optional. Specifies the parameters that are inserted into the formatted string, such as the third to fourth, etc.% symbol.

Description: 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.

35. The Quotemeta () function adds a backslash before certain predefined characters in the string.

Syntax: Quotemeta (String)

These predefined characters are:

    • Period (.)
    • Back slash (\)
    • Plus (+)
    • asterisk (*)
    • Question mark (?)
    • square brackets ([])
    • Caret character (^)
    • Dollar sign ($)
    • Parentheses (())

Unlike the addslashes () function, it escapes the predefined characters that are

    • Single quotation mark (')
    • Double quotation marks (")
    • Back slash (\)
    • Null

36. The setlocale () function sets the region information (geographic information). Region information is the language, currency, time, and other information for a geographic region. The function returns the current locale setting and returns False if it fails.

Syntax: setlocale (constant,location)

constant--required. Specify what area information should be set.

The Constants available:

    • Lc_all-Includes all the options below
    • Lc_collate-Sort order
    • Lc_ctype-character categories and conversions (for example, all characters uppercase or lowercase)
    • Lc_messages-System Message Format
    • Lc_monetary-Currency format
    • Lc_numeric-Number format
    • Lc_time-Date/Time format

location--required. Specify how the region information is set for the country/region. If the location parameter is an array, setlocale () attempts each array element until a valid language or region code is found. This is useful if a region has a different name on a different system.

Tip: the setlocale () function changes the region information only for the current script. The region information can be set to system default by SetLocale (Lc_all,null). Example

In this example, we will set the locale to US 中文版 and then set it back to the system default:

<? PHP Echo setlocale (Lc_all, "en-us"); Echo setlocale (Lc_all,NULL);? >

37. The SHA1 () function computes the SHA-1 hash of the string. The SHA1 () function uses the United States Secure Hash algorithm 1. If successful, returns the computed SHA-1 hash, or False if it fails.

Syntax: SHA1 (string,raw)

raw-- is optional. Specify hexadecimal or binary output format: TRUE-original 20 character binary format; FALSE-Default. 40 character hexadecimal number; Note: This parameter is added in PHP 5.0.

Example 1
<? PHP $str = ' Hello '; Echo SHA1 ($str);? >

Output:

F7ff9e8b7bb2e09b70935a5d785e0cc5d9d0abf0

38. The Ha1_file () function computes the SHA-1 hash of the file.

Syntax: Sha1_file (string,raw)

39, the Similar_text () function calculates the number of matching characters for two strings. The function can also calculate the similarity of two strings (in percent).

Syntax: Similar_text (string1,string2,percent)

string1--required. Specifies the first string to compare.

string2--required. Specifies a second string to compare.

percent--is optional. A variable name that specifies the similarity of the storage percentage.

Note: the Levenshtein () function is faster than the Similar_text () function. However, the Similar_text () function provides more precise results with fewer required modifications. Example Example 1
<? PHP Echo Similar_text ("Hello World", "Hello Peter"); >

Output:

7
Example 2
<? PHP Similar_text ("Hello World", "Hello Peter",$percent); Echo $percent ;? >

Output:

63.6363636364

39. The sprintf () function writes a formatted string to a variable.

Example 1
<? PHP $str = "Hello"; $number = 123; $txt sprintf ("%s world. Day number%u ",$str,$number); Echo $txt ;? >

Output:

 Number 123

40. The scanf () function parses input from a string according to the specified format.

If only two parameters are passed to the function, the data is returned as an array. Otherwise, if additional parameters are passed, the parsed data is stored in these parameters. If the number of delimiters is greater than the number of variables that contain them, an error occurs. However, if the delimiter is less than the variable, the extra variable contains NULL.

Syntax: sscanf (string,format,arg1,arg2,arg++)

string--required. Specifies the string to read.

format--required. Specifies the format to use.

arg1--is optional. The first variable that stores the data.

arg2--is optional. A second variable that stores data.

arg++--is optional. A third to fourth variable that stores data. And so on

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)
Example
<? PHP $string = "age:30 weight:60kg"; sscanf ($string, "age:%d weight:%dkg",$age,$weight); // Show types and values Var_dump ($age,$weight);? >

Output:

Int (int.) int (60)

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

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.