不少于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>";

字符串练习

更多相关文章

  1. 示例演示:数据类型的转换和检测技术、变量与常量的方式以及使用
  2. 字节尿性,康托展开求第K个排列!
  3. PHP初级知识本地搭建和常用标签类型
  4. 数值运算常用途径;字符串拼接注意事项;流程控制语句与导入外部文件
  5. 这 10 行比较字符串相等的代码给我整懵了,不信你也来看看
  6. 字符串操作 — Google Guava
  7. 细说PHP的变量与常量
  8. php学习小结(php程序的运行原理、短标签的使用与html混编、常用数
  9. PHP环境搭建与基础语法

随机推荐

  1. android 下载文件注意事项
  2. android 如何判断软件是否第一次运行?
  3. Android TextView中链接(link)点击事件的
  4. Android 动画旋转效果
  5. Android(安卓)UI开发第三十九篇——Tab界
  6. android 拖动条改变屏幕亮度
  7. Android复制手机号码到剪切板并调起打电
  8. 在Android中自定义IOS风格的按钮
  9. Android AAC框架从入门到精通
  10. android之蓝牙设备的使用01