<?php
header('content-type:text/html;charset=utf-8');
//$url = "http://localhost/xiaoer/lianxi/lainxi.php";
$str = "what are you do you think why if you have one color you well have a bock what are you do you think why if you have one color you well have a bock";
//print_r($str);
$arr = explode(' ',$str);
//print_r($arr);die;
//将数组中的重复值去掉
$atr = array_unique($arr);
//print_r($atr);
//将数组重新排列为一个新的数组
$sal = array_values($atr);
//print_r($sal);
for ($i=0; $i <count($sal) ; $i++) {
$count[]=$sal[$i].'出现次数:'.substr_count($str,$sal[$i]);
}
//print_r($count);die;
?>
<center>
<table border="1">
<?php
for ($i=0; $i <count($count) ; $i++) {
echo "<tr><td>".$count[$i]."</td></tr>";
}
?>
</table>
</center>
header('content-type:text/html;charset=utf-8');
//$url = "http://localhost/xiaoer/lianxi/lainxi.php";
$str = "what are you do you think why if you have one color you well have a bock what are you do you think why if you have one color you well have a bock";
//print_r($str);
$arr = explode(' ',$str);
//print_r($arr);die;
//将数组中的重复值去掉
$atr = array_unique($arr);
//print_r($atr);
//将数组重新排列为一个新的数组
$sal = array_values($atr);
//print_r($sal);
for ($i=0; $i <count($sal) ; $i++) {
$count[]=$sal[$i].'出现次数:'.substr_count($str,$sal[$i]);
}
//print_r($count);die;
?>
<center>
<table border="1">
<?php
for ($i=0; $i <count($count) ; $i++) {
echo "<tr><td>".$count[$i]."</td></tr>";
}
?>
</table>
</center>
本文通过一个简单的PHP脚本演示了如何统计并展示一个特定字符串中每个字符出现的次数。该脚本使用explode()函数将字符串拆分为字符数组,再利用array_unique()去除重复项,并最终通过遍历计算每个字符的出现频率。
445

被折叠的 条评论
为什么被折叠?



