Function to count number of digits in string
问题 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? 回答1: 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. 回答2: first split your string, next filter the result to only