PHP: Best way to split string into alphabetic and numeric components

后端 未结 5 1546
情歌与酒
情歌与酒 2021-01-15 15:09

I have several strings of the format

AA11
AAAAAA1111111
AA1111111

Which is the best (most efficient) way to sepa

5条回答
  •  Happy的楠姐
    2021-01-15 15:38

    If they're all a series of alpha, followed by a series of numeric, with no non-alphameric characters, then sscanf() is probably more efficient than regexp

    $example = 'AAA11111';
    list($alpha,$numeric) = sscanf($example, "%[A-Z]%d");
    
    var_dump($alpha);
    var_dump($numeric);
    

提交回复
热议问题