Search code examples
phpstringsplittext-extractiontext-parsing

Split string strictly formatted as uppercase letter then numbers into two halves


I have several strings of the format

AA11
AAAAAA1111111
AA1111111

I need to separate the alphabetic and numeric components of the string.


Solution

  • 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);