PHP: split a string of alternating groups of characters into an array
<p>I have a string whose correct syntax is the regex <code>^([0-9]+[abc])+$</code>. So examples of valid strings would be: '1a2b' or '00333b1119a555a0c'</p>

<p>For clarity, the string is a list of (value, letter) pairs and the order matters. I'm stuck with the input string so I can't change that. While testing for correct syntax seems easy in principle with the above regex, I'm trying to think of the most efficient way in PHP to transform a compliant string into a usable array something like this:</p>

<p>Input: </p>

<pre><code>'00333b1119a555a0c'
</code></pre>

<p>Output:</p>

<pre><code>array (
  0 =&gt;  array('num' =&gt; '00333', 'let' =&gt; 'b'),
  1 =&gt;  array('num' =&gt; '1119', 'let' =&gt; 'a'),
  2 =&gt;  array('num' =&gt; '555', 'let' =&gt; 'a'),
  3 =&gt;  array('num' =&gt; '0', 'let' =&gt; 'c')
)
</code></pre>

<p>I'm having difficulty using preg_match for this. For example this doesn't give the expected result, the intent being to greedy-match on EITHER \d+ (and save that) OR [abc] (and save that), repeated until end of string reached.</p>

<pre><code>$text = '00b000b0b';
$out = array();
$x = preg_match("/^(?:(\d+|[abc]))+$/", $text, $out);
</code></pre>

<p>This didn't work either, the intent here being to greedy-match on \d+[abc] (and save these), repeated until end of string reached, and split them into numbers and letter afterwards.</p>

<pre><code>$text = '00b000b0b';
$out = array();
$x = preg_match("/^(?:\d+[abc])+$/", $text, $out);
</code></pre>

<p>I'd planned to check syntax as part of the preg_match, then use the preg_match output to greedy-match the 'blocks' (or keep the delimiters if using preg_split), then if needed loop through the result 2 items at a time using <code>for (...; i+=2)</code> to extract value-letter in their pairs. </p>

<p>But I can't seem to even get that basic preg_split() or preg_match() approach to work smoothly, much less explore if there's a 'neater' or more efficient way.</p>