PHP string and array common functions

Source: Internet
Author: User
PHP string and array common function string: string connection: use an English dot in PHP to connect two strings. Remove spaces at the beginning and end of a string: trim removes spaces at both ends of a string. & Nbsp; & nbsp; & nbsp PHP string and array common functions
String:
String connection:PHP uses the English dot. to connect two strings.
Remove spaces at the beginning and end of the string:Trim removes spaces at both ends of a string.
Rtrim removes the right space of a string. r is the abbreviation of right.
Ltrim removes the left space of a string, and l is the abbreviation of left.
Obtain the length of a string:Php has a magic function that can directly obtain the length of a string. This function is strlen (). However, if you have Chinese characters, you can use the mb_strlen () function to obtain the Chinese characters in the string.
String truncation:English string truncation function substr (), function description: substr (string variable, start position, number of interceptions)
The interception function mb_substr () of the Chinese string. function description: mb_substr (string variable, position where the interception starts, number of interceptions, and webpage encoding)
String search: Query string. we need to use the strpos () function of PHP. function description: strpos (the string to be processed, the string to be located, and the starting position to be located [optional]).
String replacement: Replace the string. we need to use the replacement function str_replace () of PHP. function description: str_replace (the string to be searched, the string to be replaced, and the string to be searched, replacement for counting [optional])
String formatting: PHP formatting string function sprintf (); function description: sprintf (format, string to be converted); return: formatted string
Example:
$ Str = '99. 9'; $ result = sprintf ('% 01.2f', $ str); echo $ result; // The result is 99.90

What does % 01.2f mean?
1. this % sign indicates the beginning. it is written at the beginning to indicate that the specified format has started. That is, "Start character", until "conversion character" appears, even if the format ends.
2. 0 is followed by the % sign, which is a "character", indicating that 0 is used to fill the space if the position is empty.
3. the value after 0 is 1. this 1 indicates that all strings must have more than one placeholder (the decimal point is also a placeholder ).
If 1 is changed to 6, the value of $ result is 099.90.
Because, there must be two digits after the decimal point, and 99.90 has a total of five placeholder values. now we need six placeholder values, so we need to fill them with 0.
4. the. 2 (point 2) After % 01 is easy to understand. it means that the number after the decimal point must be two digits. If the value of $ str is 9.234 at this time, the value of $ result is 9.23.
Why is 4 missing? Because after the decimal point, according to the above rules, only two digits are allowed. However, in the $ str value, there are three digits after the decimal point. Therefore, the tail number 4 is removed, with only 23 digits left.
5. end with f "conversion character.
String merging:Php string merging function implode (); function description: implode (delimiter [optional], array); return value: combines array elements into a string
Example:
$ Arr = array ('hello', 'World! '); $ Result = implode ('', $ arr); print_r ($ result); // The result shows Hello World!

String Segmentation:Php string separator function explode (); function description: explode (separator [optional], string); return value: the function returns an array composed of strings
Example:
$ Str = 'Apple, banana '; $ result = explode (', ', $ str); print_r ($ result); // The result shows array ('apple ', 'bana ')

Character string escape: Php string escape function addslashes (); function description: used to add escape characters to special characters to return a string; return value: an escaped string
Example:
$ Str = "what's your name? "; Echo addslashes ($ str); // output: what \'s your name?

Array:
Foreach cyclically accesses the values in the index array:The foreach loop can access all values in the array. next we will show that the foreach loop can be used to access the values in the array.
For example:
$ Fruit = array ('apple', 'banana ', 'Pineapple'); foreach ($ fruit as $ k => $ v) {echo'
The value of '. $ k.' is '. $ v ;}

Join array initialization:PHP has two types of arrays: index array and associated array.
You can use the following code:
$ Fruit = array ('apple' => "apple", 'bana' => "banana", 'pineapple' => "pineapple ");


You can use print_r ($ fruit); to output the array key and the corresponding value.
Associated array assignment:There are two ways to assign values to correlated arrays:
First, assign a value using the brackets following the name of the array variable. of course, in the joined array, the keys in the brackets must be strings. For example, $ arr ['apple'] = 'apple ';

Type 2: Use array () to create an empty array and use the => symbol to separate keys and values. the left side indicates the key and the right side indicates the value. Of course, in the associated array, the key must be a string. For example, array ('apple' => 'apple ');
Access the associated array content:Use the array variable name followed by brackets + keys to access the values in the array. The Keys are enclosed in single or double quotation marks.
For example:
$ Fruit = array ('apple' => "apple", 'bana' => "banana", 'pineapple' => "pineapple "); $ fruit0 = $ fruit ['bana']; print_r ($ fruit0 );

Values of the foreach loop access associated array:The foreach loop can access all values in the array. the following shows how to use the foreach loop to access values in the associated array.
For example:
$ Fruit = array ('apple' => "apple", 'bana' => "banana", 'pineapple' => "pineapple "); foreach ($ fruit as $ k => $ v) {echo'
English fruit key name: '. $ k.', corresponding value: '. $ v ;}

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.