View Full Version : Strings

01-12-2004, 04:48 PM
I recive a string in a form like this: 200065123-19
If you add all the numbers before the dash together, the sum shall be the same as the number after the dash. Like this: 2+0+0+0+6+5+1+2+3 = 19

How can I check this?

01-12-2004, 05:35 PM
Split the string on the dash into two parts. Then, create an array out of the pre-dash part containing where each digit is in a field. After that you only need to sum up the fields of that array and compare that to the value right of the dash.

function checksum($word) {
list($numbers, $result) = split("-", $word);
$calcSum = array_sum(preg_split("//", $numbers));
return $calcSum == $result;

print checksum('200065123-19');