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."
";
echo '大写字母个数:'.$capitalLetter."
";
echo '空格个数:'.$blank."
";
echo '标点个数:'.$punctuation."
";
?>

 

转载于:https://www.cnblogs.com/Wang-Y/p/7786902.html

你可能感兴趣的:(php字符串英文文本中大写字母,小写字母,空格,标点符号的个数统计)