...

View Full Version : Strings



Pompiuses
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?

mordred
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');



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum