lets say i have those two arrays
$letters = array('a','b','c', 'd', 'e');
$replace = array( 1, 5, 10, 15 , 20);
$text = "abd cde dee ae d" ;
$re = str_replace($letters, $replace, $text) ;
echo $re ; //this output:
1515 101520 152020 120 15
Now i want sum the above numbers for each word and the result should be like that:
21 45 55 21 15
what i tried is :
$resultArray = explode(" ", $re);
echo array_sum($resultArray).'<br />' ; // but it output wrong result.
// it output this : 255190
how can i achieve this ?
any help much apreciated.
EDIT:
with arabic letters like that
$letters = array('ا', 'ب','ج','د' ) ;
$replace = array(1, 5, 10, 15 ) ;
$text = "جا باب جب";
Convert the string into an array and use
array_sum
.Edit
Sorry, misunderstood:
Instead of replacing the letters with numbers I would suggest just looking up the letters in the replace array one at a time:
EDIT
Here is a working example: http://codepad.org/Cw71zuKD