Home  >  Article  >  Backend Development  >  Summary of usage of commonly used strings in php

Summary of usage of commonly used strings in php

WBOY
WBOYOriginal
2016-07-25 08:59:041158browse
  1. echo implode(",", array('lastname', 'email', 'phone'));//Convert array to string
Copy code

explode:

  1. print_r(explode(",", 'lastname,email,phone'));//Convert string to array
Copy code

split:

  1. print_r(split("[/.-]","2008-9.12"));//Cut into array with / or . or - any symbol
Copy code

str_split:

  1. print_r(str_split("Hello Friend",1));//Split the string
Copy code

preg_split:

  1. //Regular split
  2. //$ops = preg_split("{[+*/-]}","3+5*9/2");
  3. //print_r($ops);/ /Returns: Array ([0] => 3 [1] => 5 [2] => 9 [3] => 2 )
Copy code

http_build_query:

  1. //Generate the request string after url-encoded
  2. $data = array('localhost'=>'aa',
  3. 'user'=>'bb',
  4. 'password'=> ;'cc');
  5. echo http_build_query($data);//Return: localhost=aa&user=bb&password=cc
Copy code

strtok:

  1. //Cut the string into small segments
  2. $string = "This istan examplenstring";
  3. echo strtok($string,"nt");//Return: This is
  4. echo '
    ' ;
  5. echo strtok("nt"); //When returned for the second time: an example
  6. echo '
    ';
  7. 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

  1. echo $pos = strpos('abcdef abcdaef', 'a'); // The first occurrence of the letter a, case sensitive
  2. echo $pos = strrpos('abcdef abcdeaf', 'a '); // The last position where the letter a appears, case-sensitive
Copy code

stripos: case-insensitive strripos: case insensitive

  1. echo strstr('user@exa@mple.com', '@');//Return: @exa@mple.com
Copy code

stristr: case-insensitive

  1. echo strchr('user@exa@mple.com', '@');//Return: @exa@mple.com
Copy code

strrchr: then return: @mple .com,

preg_grep:

  1. //Return the array cells matching the pattern
  2. $food = preg_grep("/^p/",array("apple","orange","pip","banana"));
  3. print_r ($food); //Return: Array ( [2] => pip )
Copy code

strtr:

  1. //Replace the found string with the specified array
  2. $arr = array("www"=>"ftp","yahoo"=>"baidu");
  3. echo strtr("www .yahoo.com",$arr);//Return: ftp.baidu.com
  4. 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:

  1. //Find the length of the initial part that is matched
  2. echo strspn("abcdefg","1234567890");//Return: 0
  3. //Find the initial part that is not matched The length of
  4. echo strcspn("abcdefg","1234567890");//Return: 7
Copy code

3, regular matching of strings preg_match:

  1. //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.
  2. if (preg_match ("/php/i", "PhP is the web scripting language of choice."))
  3. echo "exists";
  4. else
  5. echo "does not exist";
Copy code

preg_match_all:

  1. //On the contrary, it will search until the end of subject.
  2. preg_match_all("/(?(d{3})?)?(?(1)[-s])d{3}-d{4}/x",
  3. "Call 555-1212 or 1-800- 555-1212", $phones);
  4. print_r($phones[0]);//Get all phone numbers
Copy code

ereg_replace:

  1. //Replace URL with hyperlink
  2. echo ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/] ",
  3. "\0", 'This is Baidu http://www.baidu.com website.');
  4. preg_replace:Filter
  5. $search = array ("']*?>.*?'si", // Remove javascript
  6. "'<[/!]*?[^<>]* ?>'si", // Remove HTML tags
  7. "'([rn])[s]+'", // Remove whitespace characters
  8. "'&(quot|#34);'i", // Replace HTML entities
  9. "'&(amp|#38);'i",
  10. "'&(lt|#60);'i",
  11. "'&(gt|#62);'i",
  12. "' &(nbsp|#160);'i",
  13. "'&(iexcl|#161);'i",
  14. "'&(cent|#162);'i",
  15. "'&(pound|# 163);'i",
  16. "'&(copy|#169);'i",
  17. "'(d+);'e"); // Run as PHP code
  18. $replace = array ("" ,
  19. "",
  20. "\1",
  21. """,
  22. "&",
  23. "<",
  24. ">",
  25. " ",
  26. chr(161),
  27. chr(162),
  28. chr(163),
  29. chr(169),
  30. "chr(\1)");
  31. echo $text = preg_replace ($search, $replace, 'test<script>alert( "adfasdf");</script>');
Copy code

preg_quote:

  1. //Escape regular expression characters, add each one you want to add, and it conforms to the regular expression.
  2. echo preg_quote('$40 for a g3/400','/');//Return: $40 for a g3/400
Copy code

sql_regcase:

  1. //Generate regular expression for size-insensitive matching
  2. echo sql_regcase("Foo-bar.a"); //Return: [Ff][Oo][Oo]-[Bb] [Aa][Rr].[Aa]
Copy code

1 2 Next page Last page



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn