PHP string processing is an important part of PHP basics, summarizing and collating
1, simplest, string output single quotation mark and double quotation mark difference (the definition string can use both single quotation mark and double quotation mark)
1 $str= ' Hello '; 2 Echo $str"; 3 Echo "<br/>"; 4 Echo ' Str is $str ';
See, double quotes will output the string itself, and single quotes will output the string variable identifier.
2. String connection
1 $i= ' i '; 2 $love= ' love '; 3 $you= ' you '; 4 $hi=$i. $love. $you; // Connection String 5 Echo $hi;
3. Remove whitespace from string
1 $str = "string" ; 2 $str 1 = trim ( $str ); // remove both spaces 3 $str = ( $str ); // Remove the right space 4 $str = ( $str 3 ); // remove left space
4. Get the string length
1 //get the English string length2 $love= "I Love You";3 $len=strlen($love);4 Echo $len;5 Echo"<br/>";6 //get the actual length of the Chinese string7 $love= "I love you!" ";8 $len=strlen($love);9 Echo $len;Ten Echo"<br/>"; One //get the number of Chinese characters A $love= "I love you!" "; - $len=mb_strlen ($love, "UTF8"); - Echo $len;
It is important to note that the statistics in the text characters specify the encoding to be consistent with the encoding itself, otherwise garbled, the default is UTF8 encoding can not be specified
5. String interception
1 //English string interception2 $love= "I Love You";3 Echo substr($love, 2,4);4 Echo"<br/>";5 //Chinese string interception6 $love= "I love you ah Ah";7 EchoMB_SUBSTR ($love, 0,3, ' UTF8 ');
here also pay attention to the coding problem, in fact, Chinese string interception with substr function can also, just to calculate, GBK a Chinese character accounted for 2 bytes, utf8 a Chinese character accounted for 3, not good will intercept half appear garbled, just count the number of Chinese characters with Mb_substr.
6. String Lookup
1 $str = ' What's your name? ' ; 2 Echo Strpos ($str, ' name ');
This result will output 13, which is the position of the first character of the string to be searched, the original string starting from 0
7.
1 $str = ' I love China '; 2 $replace=str_replace(' China ', ' you ',$str); 3 Echo $replace;
A look will know, output I love you
8. Formatting of strings
1 $str = ' 100.1 '; 2 $result=sprintf('%01.3f ',$str); 3 Echo $result;
This result will output 100.100,%01.3f, the preceding 0 means no number is filled 0, the back. 3 for 3 decimal places, and F is a fixed format
9. String merging and segmentation
1 //Merging Strings2 $arr=Array(' Hello ', ' world! ');3 $result=implode(‘-‘,$arr);4 Print_r($result);5 Echo"<br/>";6 //Delimited String7 $str= ' Sun-moon-star ';8 $result=Explode(‘-‘,$str);9 Print_r($result);
As we can see, the delimiter can be specified by itself, after merging is a string, and the partition becomes an array
10. String Escape output
We know that single quotation marks contain double quotes, and double quotes contain single quotes that can be output as-is, but single quotation marks or double quotation marks containing double quotes to be escaped with \ Escape, so the reverse?
1 $str = "What's This?" ; 2 Echo addslashes ($str);
This will output what\ 's this? Before the output escapes.
Well, first summarize these, easy to use when the query, later use when the time to update,
PHP String Processing Summary