开发者

php some digits from string

I have the next strings:

generation_1_2
generation_2_8
generation_3_12

How can I put digits from this strings in variables? For example:

$digit1[0] is 1.
$digit1[1] is 2.
$digit1[1] is 3.
$digit1[0] is 2.
$digit1[1] is 8.
$digit1[2] is 12.

Thanks f开发者_开发知识库or your help.


function ret_int($str){
    return array_slice(explode('_', $str), 1);
}


If you want to extract the trailing numbers you can do:

if(preg_match_all('/(\d+)$/m',$input,$m)){
   // $m[1] will have all the matches.
}

working link

EDIT:

Answer to your edited question to extract both the digits:

$digit = array();
if(preg_match_all('/(\d+)\D*(\d+)$/m',$input,$m)) {
        $digit = array_merge($m[1],$m[2]);
}

Working link


If you just want to extract the digits from the strings, you can explode them like fabrik wrote ($arr = array_slice(explode('_', $str), 1);).

Then using intval, convert each of them to integers like this: $int_a = intval($str_a, 10) where $str_a is one string from the array $arr.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜