How to separate letters and digits from a string in php

后端 未结 8 2062
攒了一身酷
攒了一身酷 2020-11-29 09:37

I have a string which is combination of letters and digits. For my application i have to separate a string with letters and digits: ex:If my string is \"12jan\" i hav to ge

8条回答
  •  伪装坚强ぢ
    2020-11-29 10:07

    This works for me as per my requirement, you can edit as per yours

    function stringSeperator($string,$type_return){
    
        $numbers =array();
        $alpha = array();
        $array = str_split($string);
        for($x = 0; $x< count($array); $x++){
            if(is_numeric($array[$x]))
                array_push($numbers,$array[$x]);
            else
                array_push($alpha,$array[$x]);
        }// end for         
    
        $alpha = implode($alpha);
        $numbers = implode($numbers);
    
        if($type_return == 'number')    
        return $numbers;
        elseif($type_return == 'alpha')
        return $alpha;
    
    }// end function
    

提交回复
热议问题