Http://www.zretc.com/technologyDetail/438.html
String manipulation can be said to be a basic PHP learning is more commonly used, but also a comparison of the basis of a, then in the beginning of PHP learning, what are the strings of the operation needs our attention?
1. The difference between single and double quotation marks
In PHP, the definition of a string can be in English single quotation marks ", or you can use double quotation marks" ";
$str = ' Hello ';
echo "STR is $STR"; Run Result: STR is hello
Echo ' str is $STR '; Operation Result: STR is $STR
2. Remove whitespace from the string
Trim removes spaces at both ends of a string. Echo Trim ("space"). "
";
RTrim is to remove the right space of a string, where r is the abbreviation. echo RTrim ("Space"). "
";
LTrim is to remove the left space of a string, where L is the abbreviation for the Ieft. Echo LTrim ("Space"). "
";
3. String connection in English dot number. To connect two strings. $hello = ' Hello ';
$world = ' world ';
$hi = $hello. $world;
4. Get the length of a string
Use the Mb_strlen () function to get the Chinese length in the string. Echomb_strlen (' I Love you ', ' UTF8 ');
Use the strlen () function to get the length of the string in English. echo strlen (' Hello ');
5. Interception of strings
English string intercept function substr (string variable, start intercept position, intercept number); echo substr (' I Love You ', 2, 4);
Chinese string intercept function mb_substr (string variable, start intercept position, intercept number);
echo mb_substr (' I Love you, China ', 4, 2, ' UTF8 ');
6. Finding strings
Function Description: Strpos (the string to be processed, the string to locate, the starting position of the anchor [optional])
7. Replacing strings
Function Description: Str_replace (the string to find, the string to replace, the string to be searched, and the replacement to count [optional])
Replace the wrong Chian with China:echo str_replace (' Chian ', ' China ', ' I love Chian ');
8. Formatting strings
Function Description: sprintf (format, string to convert)
echo sprintf ('%01.3f ', ' 100.1 '); -->100.100
sprintf ('%01.2f ', ' 99.9 ');
What does this%01.2f mean?
%: Start character, which indicates the beginning of the specified format.
0: followed by the% symbol is 0, is the "fill in the blanks", that is, if the position is empty, fill with the total.
1: Specifies that the entire string occupies more than 1 digits (the decimal point is also counted as a placeholder). If you change 1 to 6, the value of the $result will be 099.90. Because, after the decimal point must be two bits, 99.90 altogether 5 occupies, now needs 6 occupies, therefore fills with the zero.
.2: The number after the decimal point must be 2 digits. If this is the $STR value is 9.234, the $result value will be 9.23.
F: End with F "convert character".
9. Escaping a string
PHP string Escape function Addslashes ()
Function Description: Used to add an escape character to a special character, returns a string
Return value: An escaped string
$str = "What ' s your name?";
echo addslashes ($STR);//output: What ' s your name?
10. Merging and splitting of strings
(1) PHP string merge function implode ():
Function Description: Implode (delimiter [optional], array)
Return value: Combine array elements into a single string
$arr = Array (' Hello ', ' world! ');
$result = Implode (' ', $arr);
Print_r ($result);//results show Hello world!
(2) PHP string separator function explode ()
Function Description: Explode (delimiter [optional], String)
Return value: function returns an array consisting of strings
$str = ' Apple,banana ';
$result = Explode (', ', $str);
Print_r ($result);//Results Display array (' Apple ', ' banana ')
The above is the PHP introductory learning commonly used in string operations, want to learn and learn more about PHP Learning knowledge please log in soft International education Group Technology Knowledge Base!
PHP Getting Started learning--string manipulation