How to split the letters and digits from a string into 2 arrays in PHP

1 Answer

0 votes
$string = "php8java21c17";

preg_match_all('/[a-zA-Z]/', $string, $matchLetters);
preg_match_all('/\d/', $string, $matchDigits);

$letters = array();
$digits = array();

$letters = str_split(implode('', $matchLetters[0]));
$digits = str_split(implode('', $matchDigits[0]));

print_r($letters);
print_r($digits);




/*
run:

Array
(
    [0] => p
    [1] => h
    [2] => p
    [3] => j
    [4] => a
    [5] => v
    [6] => a
    [7] => c
)
Array
(
    [0] => 8
    [1] => 2
    [2] => 1
    [3] => 1
    [4] => 7
)

*/

 



answered Dec 17, 2023 by avibootz
...