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?
Python String count() MethodThe count() method returns the number of times a specified value appears in the string.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With