PHP8.1.21版本已发布
vue8.1.21版本已发布
jquery8.1.21版本已发布

博客列表 > 不少于20个字符串函数练习

不少于20个字符串函数练习

葡萄枝子
葡萄枝子 原创
2021年02月01日 23:40:40 596浏览

不少于20个字符串函数练习

任选不少于20个字符串函数进行练习,从参数,返回值,应用场景上进行分析和记忆, (课堂上讲过的不得再写了)

  1. // 不少于20个字符串函数练习
  2. $str = 'title="hello world."';
  3. // 1. 添加反斜杠
  4. $test = addslashes($str);
  5. // title=\"hello world.\"
  6. echo "$test<br>";
  7. // 2. 去除反斜杠
  8. $test = stripslashes($test);
  9. // title="hello world."
  10. echo "$test<br>";
  11. // 3. 特殊字符转html实体
  12. $test = htmlspecialchars($str);
  13. // title=&quot;hello world.&quot;
  14. echo "$test<br>";
  15. // 4. html实体转普通字符
  16. $test = htmlspecialchars_decode($test);
  17. // title="hello world."
  18. echo "$test<br>";
  19. // 5. 转义元字符
  20. $test = quotemeta($str);
  21. // title="hello world\."
  22. echo "$test<br>";
  23. // 6. 格式输入解析
  24. $n = sscanf('hello world!', '%s %s', $hello, $world);
  25. // 2 : hello world!
  26. echo "$n : $hello $world<br>";
  27. // 7. 随机打乱字符串
  28. $test = str_shuffle($str);
  29. // ll e"=tdlhtro.o"wlie
  30. echo "$test<br>";
  31. // 8. 统计单词个数
  32. $test = str_word_count($str);
  33. // 3
  34. echo "$test<br>";
  35. $test = str_word_count($str, 1);
  36. // Array ( [0] => title [1] => hello [2] => world )
  37. echo print_r($test, true), '<br>';
  38. $test = str_word_count($str, 2);
  39. // Array ( [0] => title [7] => hello [13] => world )
  40. echo print_r($test, true), '<br>';
  41. // 9. 二进制安全比较
  42. // 全比较 true
  43. echo var_export(strcmp('123', 123) === 0, true), '<br>';
  44. // 只比较2个长度 true
  45. echo var_export(strncmp('123', 124, 2) === 0, true), '<br>';
  46. // 10. 字符串长度
  47. $test = strlen($str);
  48. // 20
  49. echo "$test<br>";
  50. // 11. 字符串反转
  51. $test = strrev($str);
  52. // ".dlrow olleh"=eltit
  53. echo "$test<br>";
  54. // 12. 标记分割字符串
  55. $test = strtok('php.io/0201.php', '.');
  56. // 获取扩展名 php
  57. echo "$test<br>";
  58. // 13. 字符串转大小写
  59. $test = strtoupper($str);
  60. // TITLE="HELLO WORLD."
  61. echo "$test<br>";
  62. $test = strtolower($str);
  63. // title="hello world."
  64. echo "$test<br>";
  65. // 14. 转换指定字符到字符
  66. // world 替换成 php
  67. $test = strtr($str, array('world' => 'php'));
  68. // title="hello php."
  69. echo "$test<br>";
  70. // 15. 首字大写
  71. $test = ucfirst('oops! not found.');
  72. // Oops! not found.
  73. echo "$test<br>";
  74. // 单词首字大写
  75. $test = ucwords('oops! not found.');
  76. // Oops! Not Found.
  77. echo "$test<br>";
  78. // 16. 单词分割子串
  79. $test = wordwrap('Oops! not found.', 8, "<br>");
  80. /*
  81. Oops!
  82. not
  83. found.
  84. */
  85. echo "$test<br>";

字符串练习

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议