在PHP中使用字母数字字符串上的preg_split

Val*_*sel 0 php regex alphanumeric preg-split

考虑字符串12345aaa.我想使用preg_split()它,所以只返回它的数字部分(即12345).我该如何为它编写正则表达式?

Yac*_*oby 5

preg_split不是你想要的函数,你想要的preg_match是你不想将字符串拆分成部分,而是要检索它的一部分.由于我不知道更多细节,我只能提供一个如何做的粗略示例.

$reg = null;
//match the first set of 1 or more numbers
if ( preg_match('/\d+/', $str, $reg) ){
    $num = $reg[0];
}
Run Code Online (Sandbox Code Playgroud)