Function to count number of digits in string

counting, digits, php

Solution

This can easily be accomplished with a regular expression.

function countDigits( $str )
{
    return preg_match_all( "/[0-9]/", $str );
}

The function will return the amount of times the pattern was found, which in this case is any digit.

Problem

I was looking for a quick PHP function that, given a string, would count the number of numerical characters (i.e. digits) in that string. I couldn't find one, is there a function to do this?

Original source