# Thread: too tough for me (verify this number)

1. Registered User
Join Date
Jul 2003
Posts
5

## Unanswered: too tough for me (verify this number)

BW1234561 ---> this number is mathematically correct

i need a script to verify this string of letters and numbers

step 1: this must start with an "a" or a "b" as in the example above (the second letter can be anything)

step 2: Add the 1st, 3rd, and 5th numbers (example = 9)

step 3: Add the 2nd, 4th, and 6th numbers (example = 12)

step 4: Add the results (example = 9 + 12 = 21)

step 5: The 7th number (in the example) is the last digit of the sum of step 4 (the answer is "1" as in "21")

leaving you a result of ---BW1234561

2. Registered User
Join Date
Jul 2003
Posts
2
Okay, I worked on your problem, and here's the complete script for it. I made the function for checking the string pattern so that it is using to implement whereever you like.

I haven't checked the code, but I hope it works fine, email me if it doesnt work, or u need some explanation....

Regards,
Comp.

<?php

// Coded by Asim
// compooterz [at] yahoo [dot] co [dot] uk

////////////////////////////
// CREATING FUNCTION
////////////////////////////

function checkstring(\$string)
{
\$result=0;

// First character must be a or b
switch(\$string[0])
{
case 'a':
case 'b':
case 'A':
case 'B': \$flag=1;
break;
default: \$flag=0;
}

// If first character IS a or b
if(\$flag==1)
{
\$total1=\$string[2]+\$string[4]+\$string[6];
\$total2=\$string[3]+\$string[5]+\$string[7];
\$total=\$total1+\$total2;

\$lastno=substr(\$total, -1);

if(\$lastno==\$string[8])
\$result=1;
}

// Returns 1 if string is OKAY
// Returns 0 otherwise

return \$result;

}

/////////////////////
// WORKING PART
/////////////////////
if(isset(\$input))
{
\$res=checkstring(\$input);
echo \$res;
}
else
echo "Error!";
?>

3. Registered User
Join Date
Jul 2003
Posts
5
The script worked perfectly!! Thank you very much.

4. Registered User
Join Date
Aug 2003
Location
Bologna - Italy
Posts
209
I'm no regular expressions expert, but i'm sure it can be resolved with much less code if coded using regexps...

Anybody out there that knows regexps? I'll be very curious!

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•