Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

40,011 questions

51,958 answers

573 users

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
...