PHP中最后一位的PHP位置

Bxx*_*Bxx 6 php regex

寻找一些正则表达式来返回字符串的最后一个数字位置

$str = '1h 43 dw1r2 ow';  //Should return 10
$str = '24 h382';  //Should return 6
$str = '2342645634';  //Should return 9
$str = 'Hello 48 and good3 58 see you';  //Should return 20
Run Code Online (Sandbox Code Playgroud)

这样做但我正在寻找最快的方法(例如正则表达式?)

function funcGetLastDigitPos($str){

    $arrB = str_split($str);

    for($k = count($arrB); $k >= 0; $k--){

        $value =    $arrB[$k];
        $isNumber = is_numeric($value);

        //IF numeric...
        if($isNumber) {

            return $k;
            break;
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

wol*_*ast 7

你可以找到没有数字的字符串的最后部分,计算它,然后从整个字符串的长度中减去它.

$str = '1h 43 dw1r2 ow';  //Should return 10
echo lastNumPos($str); //prints 10
$str = '24 h382';  //Should return 6
echo lastNumPos($str); //prints 6
$str = '2342645634';  //Should return 9
echo lastNumPos($str);//prints  9
$str = 'Hello 48 and good3 58 see you';  //Should return 20
echo lastNumPos($str); //prints 20


function lastNumPos($string){
    //add error testing here

    preg_match('{[0-9]([^0-9]*)$}', $string, $matches);
    return strlen($string) - strlen($matches[0]);
}
Run Code Online (Sandbox Code Playgroud)