-2

I need to use preg_split() function to split string into alpha and numeric.

Ex: ABC10000 into, ABC and 10000

GSQ39800 into GSQ and 39800

WERTYI67888 into WERTYI and 67888

Alpha characters will be the first characters(any number of) of the string always and then the numeric(any number of).

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Shaolin
  • 2,541
  • 4
  • 30
  • 41
  • 1
    Stack Overflow is not a code or regex writing service. What have you tried so far to figure this out yourself? – Ken White Jul 18 '17 at 02:17
  • @KenWhite I tried `/([a-zA-Z]+)(\d+)/` with preg_match unfortunately didn't work with preg_split. What do you think it is for then ? – Shaolin Jul 18 '17 at 02:56
  • I think it's for solving problems that people have once they've made an effort and couldn't do it. It's not a free coding service where you post your requirements and language of choice and someone does your work for you. If you *tried this*, it should be in your question to show the effort, and you should explain why it didn't satisfy your needs. If it's not in your post, it doesn't exist as far as we know. – Ken White Jul 18 '17 at 23:05

2 Answers2

2

using preg_match

$keywords = "ABC10000";
preg_match("#([a-zA-Z]+)(\d+)#", $keywords, $matches);
print_r($matches);

output

Array
(
    [0] => ABC10000
    [1] => ABC
    [2] => 10000
)
ewwink
  • 18,382
  • 2
  • 44
  • 54
1

This is a tiny task. Use \K with matching on an capital letters in a character class using a one or more quantifier:

Code:

$in='WERTYI67888';
var_export(preg_split('/[A-Z]+\K/',$in));

Output:

array (
  0 => 'WERTYI',
  1 => '67888',
)
mickmackusa
  • 43,625
  • 12
  • 83
  • 136