Php Regular Expression application, php Regular Expression
Regular Expression
1. Replace "/\ d/", "#", $ str: Regular Expression \ d number, replace with #, string
$ Str = "2 hello 5li 6lei ";
Echo preg_replace ("/\ d/", "#", $ str );
2. Split the Regular Expression and string
$ Str = "2 hello 5li 6lei ";
Var_dump (preg_split ("/\ d/", $ str ));
3. Match All strings that meet the Regular Expression
$ Str = "2 hello 5li 6lei ";
Preg_match_all ("/\ d", $ str, $ arr); // regular expression, defined string, array
Var_dump ($ arr); // match strings that meet all the regular expressions
4. Match the first regular string
$ Str = "2 hello 5li 6lei ";
Preg_match ("/\ d/", $ str, $ arr );
Var_dump ($ arr );
Generate random number
Echo rand ();
Echo "<br> ";
Echo rand (0, 10 );
Echo "<br> ";
// Obtain the current time
Echo time ();
Echo "<br> ";
// Format the display time
Echo date ("Y-m-d H: I: s", "1506494423 ");
Echo "<br> ";
Echo strtotime ("14:40:23 ");
String Functions
Remove string length
$ Str1 = "";
$ Str2 = "abcdecdcd ";
// Compare the ASC code
Var_dump (strcmp ($ str2, $ str1 ));
// Display Length
Echo strlen ($ str );
// Split the string
// Split () -- js
Echo "<br> ";
Var_dump (explode ('B', $ str2 ));
// Link character
Echo implode ('|', ["a", "B", "c"]);
Replace string search and replace
Echo str_replace ("cd", "CD", $ str2 );
Specified position replacement
Echo substr_replace ($ str2, "xxx", 0, 3 );
Truncate string
Echo substr ($ str2, 1, 3 );