 <p>I figured out the following code:</p> <pre><code>$helper = "15-37;10-38;5-39;"; $sum=0; for($i=0; $i&lt;$helper.length; $i++){ if($helper[$i]=="-"){ while($helper[$i]!=";") $i++; } else { if($helper[$i]!=";"){ $aux=""; while($helper[$i]!="-" &amp;&amp; $helper[$i]!=";"){ $aux = $aux.$helper[$i]; //echo $helper[$i]." "; $k++; $i++; } $i--; echo (int)$aux."&lt;br&gt;"; $sum = $sum + intval($aux); } } } echo $sum; </code></pre> <p>You can test it here: <a href="http://writecodeonline.com/php/" rel="nofollow">http://writecodeonline.com/php/</a> The last outputed number is the sum.</p> <p>Hope it helps!</p>