博主信息
博文 70
粉丝 1
评论 0
访问量 68750
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
不少于20个字符串函数练习
葡萄枝子
原创
804人浏览过

不少于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举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学