PHP_001 : Extracting numeric value from string and find their sum

Input : 10abc20def30ghi40jkl50mno

Output: 10 20 30 40 50 and its sum.


<?php
$str = "10abc20def30ghi40jkl50mno";
$newstr="";
for ($i=0; $i<strlen($str); $i++)
{
if(is_numeric($str[$i]))
$newstr= $newstr.$str[$i];
if($i==0) continue;
if(!is_numeric($str[$i]) && substr($newstr,-1)!=" ")
$newstr=$newstr." ";
}
echo $newstr;
$arr=explode(" ",$newstr);
echo "<br/>".array_sum($arr);
?>

Comments