批改状态:合格
老师批语:
任选不少于20个字符串函数进行练习,从参数,返回值,应用场景上进行分析和记忆, (课堂上讲过的不得再写了)
// 不少于20个字符串函数练习$str = 'title="hello world."';// 1. 添加反斜杠$test = addslashes($str);// title=\"hello world.\"echo "$test<br>";// 2. 去除反斜杠$test = stripslashes($test);// title="hello world."echo "$test<br>";// 3. 特殊字符转html实体$test = htmlspecialchars($str);// title="hello world."echo "$test<br>";// 4. html实体转普通字符$test = htmlspecialchars_decode($test);// title="hello world."echo "$test<br>";// 5. 转义元字符$test = quotemeta($str);// title="hello world\."echo "$test<br>";// 6. 格式输入解析$n = sscanf('hello world!', '%s %s', $hello, $world);// 2 : hello world!echo "$n : $hello $world<br>";// 7. 随机打乱字符串$test = str_shuffle($str);// ll e"=tdlhtro.o"wlieecho "$test<br>";// 8. 统计单词个数$test = str_word_count($str);// 3echo "$test<br>";$test = str_word_count($str, 1);// Array ( [0] => title [1] => hello [2] => world )echo print_r($test, true), '<br>';$test = str_word_count($str, 2);// Array ( [0] => title [7] => hello [13] => world )echo print_r($test, true), '<br>';// 9. 二进制安全比较// 全比较 trueecho var_export(strcmp('123', 123) === 0, true), '<br>';// 只比较2个长度 trueecho var_export(strncmp('123', 124, 2) === 0, true), '<br>';// 10. 字符串长度$test = strlen($str);// 20echo "$test<br>";// 11. 字符串反转$test = strrev($str);// ".dlrow olleh"=eltitecho "$test<br>";// 12. 标记分割字符串$test = strtok('php.io/0201.php', '.');// 获取扩展名 phpecho "$test<br>";// 13. 字符串转大小写$test = strtoupper($str);// TITLE="HELLO WORLD."echo "$test<br>";$test = strtolower($str);// title="hello world."echo "$test<br>";// 14. 转换指定字符到字符// world 替换成 php$test = strtr($str, array('world' => 'php'));// title="hello php."echo "$test<br>";// 15. 首字大写$test = ucfirst('oops! not found.');// Oops! not found.echo "$test<br>";// 单词首字大写$test = ucwords('oops! not found.');// Oops! Not Found.echo "$test<br>";// 16. 单词分割子串$test = wordwrap('Oops! not found.', 8, "<br>");/*Oops!notfound.*/echo "$test<br>";

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