user1684485
user1684485

Reputation: 105

Split string into array by letters

I'm trying to split a string into an array such that if the string is z10k4m42 it would become

array('z' => 10, 'k' => 4, 'm' => 42)

Is this possible?

Upvotes: 0

Views: 129

Answers (1)

xdazz
xdazz

Reputation: 160883

Try the below:

preg_match_all('/([a-z]+)(\d+)/', 'z10k4m42', $matches);
$ret = array_combine($matches[1], $matches[2]);

Upvotes: 6

Related Questions