Simple example code for PHP string functions

Source: Internet
Author: User
  1. /**

  2. * PHP String Function Example
  3. * by bbs.it-home.org
  4. */

  5. $str 1= "ABCD";

  6. Print ($str 1);//output function, with return result (string), Speed no echo fast!

  7. It can be said that ECHO is not a PHP function

  8. Print_r ($STR);//output function, typically used for testing
  9. echo "
    ****
    ". Ord ($str 1)."
    ";
  10. $str 2= "ABCD";
  11. echo "
    ****
    ". Ord ($str 2)."
    ";

  12. Echo strcmp ($str 2, $str 1); The ASCII value of the first letter a of//STR1 is greater than the ASCII value of the first letter of the STR2

  13. is return 1, not return-1, the same letter ASCII value continues to compare! Exactly the same return 0
  14. echo "
    ";

  15. echo Substr_count ("ABCD Bcxx", "BC");//statistics string BC in front of the character wearing the number of occurrences, no return 0

  16. echo Substr_count ("ABCD Bcxx", "BC", 3,4);//Statistics string BC from the preceding string, the 3rd character starts to the number of//,BC occurrences in the following 4 characters, no return 0
  17. echo Substr_count ("ABCD Bcxx", "BC", 3,8);//Error exceeding length

  18. echo "
    Strpos, Strrpos functions
    ";

  19. Echo Strpos ("ABCDE", "BC");//Represents the first occurrence of the BC string in the ABCDE string, no return null appears
  20. Echo Strrpos ("ABCDE", "BC1");//Indicates the last occurrence of the BC string in the ABCDE string, and no return null appears

  21. echo "
    STRSTR, STRRCHR functions
    ";

  22. Echo strstr ("abced", "BC");//output string BC starts at the first occurrence of the abced string to the following string, no return null
  23. echo "
    ";
  24. Echo strrchr ("bcabced", "1");//output string BC1 starts at the last occurrence of the abced string to the following string, does not take the first character, finds it in the preceding string, finds it, and returns if
  25. No return null found
  26. Echo strrchr ("bcabced China", "medium");

  27. echo "
    NL2BR function
    ";

  28. $str = "afdgdsa\r\n1111";
  29. echo nl2br ($STR);//convert escape carriage return to HTML

  30. echo "
    Str_repalce function
    ";

  31. echo $str = "DSFSD sdfsdf 233";
  32. echo "\ t The original string length is:". strlen ($STR). "
    ";
  33. Echo strlen (Str_replace ("", "", $str));//Find a space in the STR string instead of a string, such as finding a string that has no characters to replace, will not be manipulated!!
  34. $str = "[Dsfsdf]sdfsdf[sdfsdf]";
  35. $arr 1=array ("{", "}");
  36. $arr 2=array ("(");
  37. $str =str_replace ($arr 1, $arr 2, $STR);
  38. Echo $str;

  39. echo "
    substr function
    ";

  40. echo substr ("Abcddsfds", 2). "
    ";//a character that is truncated to the end of a string starting from the 2nd position
  41. echo substr ("Abcddsfds", 2,20);//20 characters from the beginning of the 2nd position intercept to the string

  42. echo "
    Explode, str_split functions
    ";

  43. $str = "1,2,3,4,5,";
  44. Print_r (Explode ("(", $str));//To, split string, no split character, directly return string
  45. foreach (Explode (",", $str) as $v) {
  46. echo $v. " \ t ";
  47. }
  48. $str = "1,2,3,4,5,";
  49. echo "
    ";
  50. Print_r (Explode ("2", $str, 4));
  51. echo "
    ";
  52. $STR = "Scripting Academy 22222";
  53. Print_r (Str_split ($STR, 2));//Split the string with 2 bytes, does not support the segmentation of Chinese characters
  54. ?>
Copy Code
  • 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.