-
- echo implode(",", array('lastname', 'email', 'phone'));//Convert array to string
Copy code
explode:
-
- print_r(explode(",", 'lastname,email,phone'));//Convert string to array
Copy code
split:
-
- print_r(split("[/.-]","2008-9.12"));//Cut into array with / or . or - any symbol
Copy code
str_split:
-
- print_r(str_split("Hello Friend",1));//Split the string
Copy code
preg_split:
-
- //Regular split
- //$ops = preg_split("{[+*/-]}","3+5*9/2");
- //print_r($ops);/ /Returns: Array ([0] => 3 [1] => 5 [2] => 9 [3] => 2 )
Copy code
http_build_query:
-
- //Generate the request string after url-encoded
- $data = array('localhost'=>'aa',
- 'user'=>'bb',
- 'password'=> ;'cc');
- echo http_build_query($data);//Return: localhost=aa&user=bb&password=cc
Copy code
strtok:
-
- //Cut the string into small segments
- $string = "This istan examplenstring";
- echo strtok($string,"nt");//Return: This is
- echo '
' ;
- echo strtok("nt"); //When returned for the second time: an example
- echo '
';
- echo strtok("nt"); //When returned for the third time: string
Copy code
2, search and replace of strings
Many of the strings are r: take the last one, i: case-insensitive
-
- echo $pos = strpos('abcdef abcdaef', 'a'); // The first occurrence of the letter a, case sensitive
- echo $pos = strrpos('abcdef abcdeaf', 'a '); // The last position where the letter a appears, case-sensitive
-
Copy code
stripos: case-insensitive
strripos: case insensitive
-
- echo strstr('user@exa@mple.com', '@');//Return: @exa@mple.com
-
Copy code
stristr: case-insensitive
-
- echo strchr('user@exa@mple.com', '@');//Return: @exa@mple.com
-
Copy code
strrchr: then return: @mple .com,
preg_grep:
-
- //Return the array cells matching the pattern
- $food = preg_grep("/^p/",array("apple","orange","pip","banana"));
- print_r ($food); //Return: Array ( [2] => pip )
Copy code
strtr:
-
- //Replace the found string with the specified array
- $arr = array("www"=>"ftp","yahoo"=>"baidu");
- echo strtr("www .yahoo.com",$arr);//Return: ftp.baidu.com
- echo strtr("www.yahoo.com","wo","sx");//Return: sss.yahxx.cxm Translation For strings, replace all w's with s and all o's with x
Copy code
strspn:
-
- //Find the length of the initial part that is matched
- echo strspn("abcdefg","1234567890");//Return: 0
- //Find the initial part that is not matched The length of
- echo strcspn("abcdefg","1234567890");//Return: 7
Copy code
3, regular matching of strings
preg_match:
-
- //Returns the number of times pattern is matched. Either 0 times (no match) or 1 time, since preg_match() will stop searching after the first match.
- if (preg_match ("/php/i", "PhP is the web scripting language of choice."))
- echo "exists";
- else
- echo "does not exist";
Copy code
preg_match_all:
-
- //On the contrary, it will search until the end of subject.
- preg_match_all("/(?(d{3})?)?(?(1)[-s])d{3}-d{4}/x",
- "Call 555-1212 or 1-800- 555-1212", $phones);
- print_r($phones[0]);//Get all phone numbers
Copy code
ereg_replace:
-
- //Replace URL with hyperlink
- echo ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/] ",
- "\0", 'This is Baidu http://www.baidu.com website.');
- preg_replace:Filter
- $search = array ("''si", // Remove javascript
- "'<[/!]*?[^<>]* ?>'si", // Remove HTML tags
- "'([rn])[s]+'", // Remove whitespace characters
- "'&(quot|#34);'i", // Replace HTML entities
- "'&(amp|#38);'i",
- "'&(lt|#60);'i",
- "'&(gt|#62);'i",
- "' &(nbsp|#160);'i",
- "'&(iexcl|#161);'i",
- "'&(cent|#162);'i",
- "'&(pound|# 163);'i",
- "'&(copy|#169);'i",
- "'(d+);'e"); // Run as PHP code
- $replace = array ("" ,
- "",
- "\1",
- """,
- "&",
- "<",
- ">",
- " ",
- chr(161),
- chr(162),
- chr(163),
- chr(169),
- "chr(\1)");
- echo $text = preg_replace ($search, $replace, 'test<script>alert( "adfasdf");</script>');
Copy code
preg_quote:
-
- //Escape regular expression characters, add each one you want to add, and it conforms to the regular expression.
- echo preg_quote('$40 for a g3/400','/');//Return: $40 for a g3/400
Copy code
sql_regcase:
-
- //Generate regular expression for size-insensitive matching
- echo sql_regcase("Foo-bar.a"); //Return: [Ff][Oo][Oo]-[Bb] [Aa][Rr].[Aa]
Copy code
1 2 Next page Last page
|