Page 1 of 1

match key to formula

Posted: Wed Feb 16, 2011 5:30 pm
by ad-watch
Hello php developpers,

So, i want to use a formula to match a string.

ex.

Code: Select all

$key=Q1D
$token = "Q1D36FL1HSAWLSTK8QL4B2Z27PRNYCPT5SNTXGQQAEDY32M9PY4FQKQ8OW5X81PHBAPQR9MA";

$token[0] = $a1;
$token[1] = $a2;
$token[3] = $a3;

$formula = "$a1.$a2.$a3"; <--[u]FETCHED FROM MYSQL[/u]

$key==$formula;
OUTPUT: $a1.$a2.$a3

Instead I want: Q1D

i want to define each character in $token as a variable, and use $formula to turn into previous variable. Which will then compare $formula with $KEY
Thanks.

Re: (URGENT HELP) match key to formula

Posted: Wed Feb 16, 2011 5:47 pm
by Jonah Bron
You mean like this?

Code: Select all

$key = 'Q1D';

$token = 'Q1D36FL1HSAWLSTK8QL4B2Z27PRNYCPT5SNTXGQQAEDY32M9PY4FQKQ8OW5X81PHBAPQR9MA';

$firstThree = substr($token, 0, 3);

if ($key == $firstThree) {
    // do something
}
substr() extracts a small segment of a larger string. In this case we use it to get the first three characters of the token.

http://php.net/substr

Re: (URGENT HELP) match key to formula

Posted: Wed Feb 16, 2011 6:08 pm
by ad-watch
Jonah Bron wrote:You mean like this?

Code: Select all

$key = 'Q1D';

$token = 'Q1D36FL1HSAWLSTK8QL4B2Z27PRNYCPT5SNTXGQQAEDY32M9PY4FQKQ8OW5X81PHBAPQR9MA';

$firstThree = substr($token, 0, 3);

if ($key == $firstThree) {
    // do something
}
substr() extracts a small segment of a larger string. In this case we use it to get the first three characters of the token.

http://php.net/substr
i want to use the formula to convert into a specific part of the token. and compare it with the key

ex:
Token: Q1D36FL1HSAWLSTK8QL4B2Z27PRNYCPT5SNTXGQQAEDY32M9PY4FQKQ8OW5X81PHBAPQR9MA'
Formula 1) a1.a2.a3 with output: Q1D
2) a1.a2.a8 with output: Q11

Brian has a formula of: a1.a2.a3 (as password)
Brian knows the generated token and inputs the position of the token.
Brian ENTERS: Q1D ($mytoken)

$token==$formula

But it's false since $formula will be define as a1.a2.a3 and not Q1D

I know it's tricky. please help

Re: (URGENT HELP) match key to formula

Posted: Wed Feb 16, 2011 6:34 pm
by Jonah Bron
I don't understand what the "formula" is, and how it correlates to data in the token. What is "a1.a2.a3", and what does it represent?

Re: (URGENT HELP) match key to formula

Posted: Thu Feb 17, 2011 4:41 pm
by danwguy
He was right the first time with the substr() you need to set

Code: Select all

$a1 = substr($token,0,1); 
$a2 = substr($token,1,1); 
$a3 = substr($token,2,1);
echo $a1 . $a2 . $a3; 
will output Q1D

Re: (URGENT HELP) match key to formula

Posted: Thu Feb 17, 2011 5:04 pm
by Jonah Bron
But this:

Code: Select all

$a1 = substr($token,0,1);
$a2 = substr($token,1,1);
$a3 = substr($token,2,1);
echo $a1 . $a2 . $a3; 
Is exactly the same as this:

Code: Select all

$aAllThree = substr($token, 0, 3);
echo $aAllThree;

Re: (URGENT HELP) match key to formula

Posted: Thu Feb 17, 2011 5:32 pm
by danwguy
I thought he was trying to save them as individual values so he could do things like $a1 . $a4 . $a10 but if he needs to group them up then, ya your solution is a lot easier and quicker.

Re: (URGENT HELP) match key to formula

Posted: Thu Feb 17, 2011 5:44 pm
by danwguy
From his posts above he wants to save each character in that array as a variable $a[n] so the best way is to store them using

Code: Select all

$a1 = substr($token,0,1);
$a2 = substr($token,1,1);
//and so on
//then you can use them however you see fit, just dont use quotes when referring to them like you did in the first post
$formula = $a1 . $a2 . $a[n]
unless you change $token so it's an array. Then you can use your first method, just get rid of the quotes around your $a1 . $a2 . $a3. it might actually be easier to store $token as an array then use $a1 = $token[0]; and so on.

Re: (URGENT HELP) match key to formula

Posted: Thu Feb 17, 2011 7:42 pm
by Jonah Bron
There's no reason to. It's the same thing, only more confusing and less efficient. And it is possible to get characters from a string by index.

Code: Select all

$string = 'abcdefghijklmnopqrstuvwxyz';
echo $string[6]; // outputs "g"

Re: (URGENT HELP) match key to formula

Posted: Fri Feb 18, 2011 1:11 pm
by danwguy
touche good sir, you have bested me again.

Re: match key to formula

Posted: Fri Feb 18, 2011 1:13 pm
by John Cartwright
In the future, please do not include things such as URGENT. Doing so implies your thread is more urgent than anyone else, which is incorrect. We prioritize everyone the same.

Thanks.