php字符串英文文本中大写字母,小写字母,空格,标点符号的个数统计

时间:2023-02-23 23:59:18

对一段英文文本的信息,统计其中大写字母,小写字母,空格,标点符号的个数

<?php
$manuscript = "Where there is a will, there is a way.";//字符串文本
$smallLetter = 0;
$capitalLetter = 0;
$blank = 0;
$punctuation = 0;

$num=strlen($manuscript);
$arr=str_split($manuscript);//字符串分割为数组
foreach($arr as $key=>$value)
{
if($value==' ')
{
$blank+=1;
}
if('a'<=$value&&$value<='z')
{
$smallLetter+=1;
}
if('A'<=$value&&$value<='Z')
{
$capitalLetter+=1;
}
}
$punctuation=$num-$smallLetter-$capitalLetter-$blank;


echo '小写字母个数:'.$smallLetter."<br>";
echo '大写字母个数:'.$capitalLetter."<br>";
echo '空格个数:'.$blank."<br>";
echo '标点个数:'.$punctuation."<br>";
?>