PHP Extract numbers from a string

前端 未结 8 1565
傲寒
傲寒 2021-01-28 08:23

I want to extract numbers from a string in PHP like following :

if the string = \'make1to6\' i would like to extract the numeric charac

8条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-28 08:35

    You can use regular expressions.

    $string = 'make1to6';
    if (preg_match('/(\d{1,10})to(\d{1,10})/', $string, $matches)) {
        $number1 = (int) $matches[1];
        $number2 = (int) $matches[2];
    } else {
        // Not found...
    }
    

提交回复
热议问题