One, string processing
1. String length strlen ("string");
2. Compare strings for the same, case-sensitive strcmp ("String 1", "String 2"); The same returns 0, string 1< string 2 returns less than 0, string 1> string 2 returns greater than 0,
3. Compare strings are identical, case insensitive strcasecmp ("String 1", "String 2") return 0, String 1< string 2 returns less than 0, string 1> string 2 returns greater than 0,
4. Case conversion, turn lowercase srttolower ("string"); Turn Capital Strtoupper ("string");
5. Split the string explode ("Split based character", "string to split"); Returns an array
$arr Explode ("|", "a|b|c|d");
The returned array is a,b,c,d
6. Merge string implode ("stitching character", "string array"); return string
$arr Array (A,b,c,D); implode ("*", "$arr");
Returns the string a*b*c*d
7. Replace the specified position substr_replace ("string to replace", "Replace with string", "Start position", "replace length"); return string
Substr_replace ("HelloWorld", "* *", "3", "2");
Back to Hel**world, note: PHP index starting from 0
8. Find Replacement Str_replace ("Find What", "replace what", "action string", "replacement number (optional parameter, receive with variable)"); return string
Str_replace ("L", "*", "HelloWorld", $i);
Return to He**owor*d, $i to 3
9. Intercept the string substr ("manipulated string", "Start position", "intercept Length"); Return string, note: A Chinese is three length, the length of the interception of Chinese is a multiple of 3, or the end will appear garbled
Regular expressions
Delimiter:/Regular/
Match start: ^
Match end: $
2 appears any time
2 appears one or more times, at least once
2? 2 appears 0 times or 1 times
A{n} A appears 3 times
A{2,} A appears at least two times and is limited to
a{2,6} A appears at least two times, up to 6 times
A|b match A or b
[ABC] matches a or B or C
[^ABC] Matches A, B, c
[A-z] matches any one character between A and Z
\d matches a numeric character
\ n line break \ r enter \ t tab
\s matches any invisible character
\s matches any visible character
\w matches any word character that contains an underscore
() First run the parentheses inside the
String regular function
1. Pattern modifier I, written after the latter delimiter, indicates that the match in and pattern is not case-sensitive,
2. Regular substitution, preg_replace ("regular", "substituted character", "string"); The lookup string satisfies the regular substitution to #, the regular expression is written in double quotation marks, the return string
3. Regular split, Preg_split ("Regular", "string"); Returns an array based on a regular split string
4. Get content that satisfies the regular, Preg_match_all ("Regular", "string", "hold Array"); Gets the contents of the string that satisfy the regular expression in the array,
$str file_get_contents ("Web address"); Preg_match_all("regular",$str,$arr);
File_get_contents ("web address"); Represents getting the Web page source code, the above code means getting the content of the Web page that satisfies the regular expression and storing it in the array $arr.
5. Match the regular elements in the array, Preg_grep ("regular", array); Returned is also an array,
PHP String Functions