Counting Spaces,Numbers and Alphabets in PHP

Source code :

<?php

# return an integer value of number of "Spaces" in the givein input
function countSpaces($data)
{
	$count=0;
	for ($i=0; $i < strlen($data); $i++) { 
		if($data[$i]==" ")
			$count+=1;
	}
	return $count;
}


# return an integer value of number of "Numbers" in the givein input
function countNumbers($data)
{
	$data=preg_replace("/[^0-9]/", "", $data);
	return strlen($data);
}


# return an integer value of number of "Alpabets" in the givein input
function countAlpabets($data)
{
	$data=preg_replace("/[^A-Za-z]/", "", $data);
	return strlen($data);
}

$demo_text = "Hello  123";

echo "There are ".countSpaces($demo_text)." spaces, ".countNumbers($demo_text)." numbers and ".countAlpabets($demo_text)." alphabets";

?>

Output :

There are 2 spaces, 3 numbers and 5 alphabets

Output :


                

Comments :