Try this:
<?php
$input = 'UO2(CO3)';
$find = array('(', ')');
$input_clean = str_replace($find, "", $input); // replace '(' and ')' with nothing
$letters = str_split($input_clean);
$letter_counter = array();
$last_char = null;
foreach ($letters as $char) {
if (preg_match("/\d+/", $char)) {
$letter_counter["$last_char"] = $letter_counter["$last_char"] + $char - 1;
}
else {
$letter_counter["$char"]++;
}
$last_char = $char;
}
var_dump($letter_counter);
?>
Quickly whipped it up but it works =)
Allen
Wow...just realized that I was a couple of months too late. Darn!