Home >Backend Development >PHP Tutorial >Simple example code for php string function

Simple example code for php string function

WBOY
WBOYOriginal
2016-07-25 08:55:50879browse
  1. /**

  2. * PHP string function example
  3. * by bbs.it-home.org
  4. */

  5. $str1="aBCD";

  6. print($str1);//Output The function returns a result (string) and is not as fast as echo!

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

  8. print_r($str);//Output function, generally used for testing
  9. echo "
    ****
    " .ord($str1)."
    ";
  10. $str2="aBCD";
  11. echo "
    ****
    ".ord($str2)."
    ";

  12. echo strcmp($str2,$str1);//Whether the ASCII code value of the first letter a of str1 is greater than the ASCII code value of the first letter of str2

  13. //is return 1, instead of returning -1, the same letter ASCII code value continues to be compared! Exactly the same and returns 0
  14. echo "
    ";

  15. echo substr_count("abcd bcxx","bc");//Count the number of times the string bc appears in front of the character , no 0 is returned

  16. //echo substr_count("abcd bcxx","bc",3,4);//The statistical string bc starts from the 3rd character in the previous string to the next 4 characters// , the number of occurrences of bc does not return 0
  17. //echo substr_count("abcd bcxx","bc",3,8);//exceeded length error

  18. echo "
    strpos, strrpos function
    ";

  19. echo strpos("abcde","bc");//The position of the first occurrence of the bc string represented in the abcde string, if it does not appear, return null
  20. echo strrpos( "abcde","bc1");//Indicates the position where the bc string last appeared in the abcde string. If it does not appear, return null

  21. echo "
    strstr, strrchr function< ;Br>";

  22. //echo strstr("abced","bc");//The output string bc starts from the position where the abced string first appears to the following string, and does not return null
  23. echo "< br>";
  24. echo strrchr("bcabced","1");//The output string bc1 starts from the last occurrence of the abced string to the following string, without taking the first character, and proceeds in the previous string Search, find and return, if it is still not found, return null
  25. echo strrchr("bcabcedChina","中");

  26. echo "
    nl2br function
    ";

  27. $str="afdgdsarn1111";
  28. echo nl2br($str);//Convert escaped carriage return, line feed, etc. to html

  29. echo "";

  30. echo $str=" dsfsd sdfsdf 233 ";
  31. echo "tThe length of the original string is: ".strlen($str)."
    ";
  32. echo strlen( str_replace(" ","",$str));//Look for spaces in str string and replace them with non-existent strings. If the searched string does not have characters to be replaced, no operation will be performed! !
  33. $str="[dsfsdf]sdfsdf[sdfsdf]";
  34. $arr1=array("{","}");
  35. $arr2=array("(");
  36. $str=str_replace($arr1,$ arr2,$str);
  37. echo $str;

  38. echo "
    substr function
    ";

  39. echo substr("abcddsfds",2)."
  40. echo substr("abcddsfds",2,20);//Intercept from the 2nd position to the 20 characters of the string
  41. echo "
    explode, str_split function
    ";

  42. $str="1,2,3,4,5,";
  43. print_r(explode("(",$str ));//Split the string with, without splitting characters, return the string directly
  44. foreach(explode(",",$str) as $v){
  45. echo $v."t";
  46. }
  47. $ str="1,2,3,4,5,";
  48. echo "
    ";
  49. print_r(explode("2",$str,4));
  50. echo "
    ";
  51. $str="Script Academy 22222";
  52. print_r(str_split($str,2));//Split the string by 2 bytes, splitting of Chinese characters is not supported
  53. ?>
Copy code

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