批改状态:未批改
老师批语:
<?php
header("content-type:text/html;charset=utf-8");
/**
* 获取字符串的长度
*/
echo '<h3>strlen($str)</h3>';
$str = 'abcdef';
$str2 = '中国人';
echo $str . ' 的长度为' . strlen($str), "<br><br>";
echo $str2 . ' 的长度为' . strlen($str2);
echo '<hr>';
/**
* 获取内部字符编码集
*/
echo '<h3>mb_internal_encoding()</h3>';
$encoding = mb_internal_encoding();
echo "内部字符编码为{$encoding}";
echo '<hr>';
/**
* 获取字符串长度
*/
echo '<h3>mb_strlen($str,"utf-8")</h3>';
$str = '中国人';
echo $str . ' 的长度为' . mb_strlen($str, 'utf-8');
echo '<hr>';
/**
* 字符串比较
*/
$str1 = 'hello';
$str2 = 'hello';
echo '<h3>strcmp($str1,$str2)</h3>';
echo strcmp($str1, $str2) == 0 ? '相等' : '不相等';
echo '<h3>strncmp($str1,$str2,length)</h3>'; // 比较str1和str2前几位的长度的字符是否相等
echo strncmp($str1, $str2, 3) == 0 ? '相等' : '不相等';
echo '<h3>strcasecmp($str1,$str2[,length])</h3>'; // 比较str1和str2不区分大小写是否相等
$str1 = 'Hello';
$str1 = 'hello';
echo strcasecmp($str1,$str2) ? '相等' : '不相等';
echo '<h3>strspn($str, $mark[, $start, $length])</h3>'; // 计算字符串中的全部字符都存在于指定字符集合的第一段子串的长度
echo strspn('15447854789', '0123456789'),'<br>';
echo strspn('15447854789', '0123456789',4,4),'<br>';
echo strspn('15447854789 458987 1125478', '0123456789');
/**
* 字符串转为数组
*/
echo '<h3>explode($delimit,$str)</h3>';
$str = '1,2,3,4,5,6';
echo '<pre>';
print_r(explode(',',$str));
echo '<hr>';
/**
* 数组转为字符串
*/
echo '<h3>implode($glue,$arr) / join($glue,$arr)</h3>';
$arr = [1,2,3,4,5];
echo implode(',',$arr);
echo '<hr>';
/**
* 字符串替换
*/
echo '<h3>str_replace($search,$replace,$str)</h3>';
$str = '中华人民共和国';
echo str_replace('国','国万岁',$str);
/**
* 字符串查找
*/
echo '<h3>strpos($str,$needle)</h3>';
$str = '中华人民共和国';
echo strpos($str,'国');
echo '<hr>';点击 "运行实例" 按钮查看在线实例
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号