Extract a single integer from a string

后端 未结 21 2476
一个人的身影
一个人的身影 2020-11-21 23:33

I want to extract the digits from a string that contains numbers and letters like:

"In My Cart : 11 items"

I want to extract the nu

21条回答
  •  无人及你
    2020-11-21 23:58

    for utf8 str:

    function unicodeStrDigits($str) {
        $arr = array();
        $sub = '';
        for ($i = 0; $i < strlen($str); $i++) { 
            if (is_numeric($str[$i])) {
                $sub .= $str[$i];
                continue;
            } else {
                if ($sub) {
                    array_push($arr, $sub);
                    $sub = '';
                }
            }
        }
    
        if ($sub) {
            array_push($arr, $sub); 
        }
    
        return $arr;
    }
    

提交回复
热议问题