Blogger Information
Blog 47
fans 3
comment 0
visits 38234
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
20个PHP字符串函数实例
Original
900 people have browsed it

  1. <?php
  2. // 1.指定的字符前添加反斜杠
  3. $str = 'Hello World!';
  4. // 向字符串中的特定字符添加反斜杠
  5. echo(addcslashes($str,'H')),'<br>';
  6. // 向字符串中的一个范围内的字符添加反斜杠
  7. $str = 'Hello World!';
  8. echo(addcslashes($str,'W..d')),'<br>';
  9. // 2.去除反斜杠
  10. $tips = addcslashes($str,'W..d');
  11. echo stripslashes($tips),'<br>';
  12. // 3.特殊字符转html实体
  13. $str1 = 'style="color:red;"';
  14. $tips = htmlspecialchars($str1);
  15. echo $tips,'<br>';
  16. // 4.html字符转html实体
  17. $tips = htmlspecialchars_decode($str1);
  18. echo $tips, '<br>';
  19. // 5.转义元字符
  20. $str1 = '1 + 1 =2';
  21. echo quotemeta($str1),'<br>';
  22. // 6.格式输入解析
  23. $a = sscanf('hello world','%s %s',$hello,$world);
  24. echo "$a : $hello , $world",'<br>';
  25. // 7.随机打乱字符串
  26. $tips = str_shuffle($str);
  27. echo $tips,'<br>';
  28. // 8.统计单词个数
  29. $tips = str_word_count($str);
  30. echo $tips,'<br>';
  31. // 9.二进制安全比较
  32. // 全比较
  33. echo var_export(strcmp('123',123) === 0,true),'<br>';
  34. // 只比较二个长度
  35. echo var_export(strncmp('883',888,2) === 0,true),'<br>';
  36. // 10.字符串长度
  37. $tips = strlen($str);
  38. echo $tips,'<br>';
  39. // 11.字符串反转
  40. $tiss = strrev($str);
  41. echo $tiss,'<br>';
  42. // 12.标记分割字符串
  43. $tips = strtok('php.cn/hello.php','.');
  44. echo $tips,'<br>';
  45. // 13.字符串转大小写
  46. $tips = strtoupper($str);
  47. echo $tips,'<br>';
  48. $tips = strtolower($str);
  49. echo $tips,'<br>';
  50. // 14.转换字符串中的特定字符
  51. $tips = strtr($str,['World' => 'php']);
  52. echo $tips,'<br>';
  53. // 15.首字母大写
  54. $tips = ucfirst('macbook');
  55. echo $tips,'<br>';
  56. // 16.单词首字大写
  57. $tips = ucwords('macbook pro');
  58. echo $tips,'<br>';
  59. // 17.使一个字符串的第一个字符小写
  60. echo lcfirst('Hello World!'),'<br>';
  61. // 18.单词分割子串
  62. $tips = wordwrap('macbook air max',3,'<br>');
  63. echo $tips;
  64. ?>
Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments