用于计算字符串中的位数的函数

时间:2022-09-23 17:30:11

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?

我正在寻找一个快速的PHP函数,给定一个字符串,将计算该字符串中的数字字符(即数字)的数量。我找不到一个,有没有这样做的功能?

4 个解决方案

#1


45  

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


7  

first split your string, next filter the result to only include numeric chars and then simply count the resulting elements.

首先拆分你的字符串,然后过滤结果只包含数字字符,然后简单地计算结果元素。

<?php 

$text="12aap33";
print count(array_filter(str_split($text),'is_numeric'));

edit: added a benchmark out of curiosity: (loop of 1000000 of above string and routines)

编辑:出于好奇心添加了一个基准:(上述字符串和例程的1000000循环)

preg_based.php is overv's preg_match_all solution

preg_based.php是overv的preg_match_all解决方案

harald@Midians_Gate:~$ time php filter_based.php 

real    0m20.147s
user    0m15.545s
sys     0m3.956s

harald@Midians_Gate:~$ time php preg_based.php 

real    0m9.832s
user    0m8.313s
sys     0m1.224s

the regular expression is clearly superior. :)

正则表达明显优越。 :)

#3


4  

For PHP < 5.4:

对于PHP <5.4:

function countDigits( $str )
{
    return count(preg_grep('~^[0-9]$~', str_split($str)));
}

#4


0  

This function goes through the given string and checks each character to see if it is numeric. If it is, it increments the number of digits, then returns it at the end.

此函数遍历给定的字符串并检查每个字符以查看它是否为数字。如果是,则增加位数,然后在结尾处返回。

function countDigits($str) {
    $noDigits=0;
    for ($i=0;$i<strlen($str);$i++) {
        if (is_numeric($str{$i})) $noDigits++;
    }
    return $noDigits;
}

#1


45  

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


7  

first split your string, next filter the result to only include numeric chars and then simply count the resulting elements.

首先拆分你的字符串,然后过滤结果只包含数字字符,然后简单地计算结果元素。

<?php 

$text="12aap33";
print count(array_filter(str_split($text),'is_numeric'));

edit: added a benchmark out of curiosity: (loop of 1000000 of above string and routines)

编辑:出于好奇心添加了一个基准:(上述字符串和例程的1000000循环)

preg_based.php is overv's preg_match_all solution

preg_based.php是overv的preg_match_all解决方案

harald@Midians_Gate:~$ time php filter_based.php 

real    0m20.147s
user    0m15.545s
sys     0m3.956s

harald@Midians_Gate:~$ time php preg_based.php 

real    0m9.832s
user    0m8.313s
sys     0m1.224s

the regular expression is clearly superior. :)

正则表达明显优越。 :)

#3


4  

For PHP < 5.4:

对于PHP <5.4:

function countDigits( $str )
{
    return count(preg_grep('~^[0-9]$~', str_split($str)));
}

#4


0  

This function goes through the given string and checks each character to see if it is numeric. If it is, it increments the number of digits, then returns it at the end.

此函数遍历给定的字符串并检查每个字符以查看它是否为数字。如果是,则增加位数,然后在结尾处返回。

function countDigits($str) {
    $noDigits=0;
    for ($i=0;$i<strlen($str);$i++) {
        if (is_numeric($str{$i})) $noDigits++;
    }
    return $noDigits;
}