Summary of common string usage in php

Source: Internet
Author: User
Tags strtok
Summary of common string usage in php

  1. Echo implode (",", array ('lastname', 'Email ', 'phone'); // Convert the array to a string

Explode:

  1. Print_r (explode (",", 'lastname, email, phone'); // convert a string to an array

Split:

  1. Print_r (split ("[/.-]", "2008-9.12"); // cut the string into an array with a sign of "/" or "." or "-"

Str_split:

  1. Print_r (str_split ("Hello Friend", 1); // cut the string

Preg_split:

  1. // Regular expression segmentation
  2. // $ Ops = preg_split ("{[+ */-]}", "3 + 5*9/2 ");
  3. // Print_r ($ ops); // return: Array ([0] => 3 [1] => 5 [2] => 9 [3] => 2)

Http_build_query:

  1. // Generate the request string after url-encoded
  2. $ Data = array ('localhost' => 'A ',
  3. 'User' => 'BB ',
  4. 'Password' => 'CC ');
  5. Echo http_build_query ($ data); // return: localhost = aa & user = bb & password = cc

Strtok:

  1. // Cut the string into segments
  2. $ String = "This is \ tan example \ nstring ";
  3. Echo strtok ($ string, "\ n \ t"); // return: This is
  4. Echo '';
  5. Echo strtok ("\ n \ t"); // when the second return: an example
  6. Echo '';
  7. Echo strtok ("\ n \ t"); // when the third return: string

2. many search and replace strings are r: final, I: case-insensitive

  1. Echo $ pos = strpos ('abcdef abcdaef ', 'A'); // The location where the first letter a appears, Case Sensitive
  2. Echo $ pos = strrpos ('abcdef abcdeaf', 'A'); // The last occurrence location of letter a, Case Sensitive

Stripos: case-insensitive strripos: case-insensitive

  1. Echo strstr ('User @ exa@mple.com ',' @ '); // return: @ exa@mple.com

Stristr: case insensitive

  1. Echo strchr ('User @ exa@mple.com ',' @ '); // return: @ exa@mple.com

Strrchr: returns @ mple.com,

Preg_grep:

  1. // Returns the array unit that matches the pattern.
  2. $ Food = preg_grep ("/^ p/", array ("apple", "orange", "pip", "banana "));
  3. Print_r ($ food); // return: Array ([2] => pip)

Strtr:

  1. // Replace the string with the specified array
  2. $ Arr = array ("www" => "ftp", "yahoo" => "baidu ");
  3. Echo strtr ("www.yahoo.com", $ arr); // return value: ftp.baidu.com
  4. Echo strtr ("www.yahoo.com", "wo", "sx"); // return: sss. yahxx. cxm translation string replace all w with s and replace all o with x

Strspns:

  1. // Find the length of the first part
  2. Echo strspn ("abcdefg", "1234567890"); // return value: 0
  3. // Find the length of the initial part that is not compared
  4. Echo strcspn ("abcdefg", "1234567890"); // return value: 7

3. string regular match preg_match:

  1. // Returns the number of times that pattern matches. Either 0 (no match) or 1 time, because 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 ";

Preg_match_all:

  1. // On the contrary, the end of the subject will be searched all the time.
  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]); // Obtain all phone numbers

Ereg_replace:

  1. // Replace the URL with the hyperconnection
  2. Echo ereg_replace ("[[: alpha:] +: // [^ <> [: space:] + [[: alnum:]/]",
  3. "\ 0", 'this is Baidu http://www.baidu.com website. ');
  4. Preg_replace: filter
  5. $ Search = array ("' ] *?>. *? Script 'Si ", // remove javascript
  6. "'<[\/\!] *? [^ <>] *?> 'Si ", // remove the HTML tag
  7. "'([\ R \ n]) [\ s] +'", // remove the white space
  8. "'& (Quot | #34);' I", // replaces the HTML object
  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 (1, 161 ),
  27. Chr (1, 162 ),
  28. Chr (1, 163 ),
  29. Chr (1, 169 ),
  30. "Chr (\ 1 )");
  31. Echo $ text = preg_replace ($ search, $ replace ,'TestScript alert ("adfasdf"); script ');

Preg_quote:

  1. // Escape the regular expression character. add \ to each character to conform to the regular expression.
  2. Echo preg_quote ('$40 for a g3/100','/'); // return: \ $40 for a g3 \/400

SQL _regcase:

  1. // Generate a regular expression for matching with no difference in size
  2. Echo SQL _regcase ("Foo-bar.a"); // return: [Ff] [Oo] [Oo]-[Bb] [Aa] [Rr]. [Aa]

12. Next page

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.