match key to formula

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ad-watch
Forum Newbie
Posts: 2
Joined: Wed Feb 16, 2011 5:17 pm

match key to formula

Post 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.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: (URGENT HELP) match key to formula

Post 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
ad-watch
Forum Newbie
Posts: 2
Joined: Wed Feb 16, 2011 5:17 pm

Re: (URGENT HELP) match key to formula

Post 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
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: (URGENT HELP) match key to formula

Post 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?
danwguy
Forum Contributor
Posts: 256
Joined: Wed Nov 17, 2010 1:09 pm
Location: San Diego, CA

Re: (URGENT HELP) match key to formula

Post 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
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: (URGENT HELP) match key to formula

Post 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;
danwguy
Forum Contributor
Posts: 256
Joined: Wed Nov 17, 2010 1:09 pm
Location: San Diego, CA

Re: (URGENT HELP) match key to formula

Post 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.
danwguy
Forum Contributor
Posts: 256
Joined: Wed Nov 17, 2010 1:09 pm
Location: San Diego, CA

Re: (URGENT HELP) match key to formula

Post 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.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: (URGENT HELP) match key to formula

Post 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"
danwguy
Forum Contributor
Posts: 256
Joined: Wed Nov 17, 2010 1:09 pm
Location: San Diego, CA

Re: (URGENT HELP) match key to formula

Post by danwguy »

touche good sir, you have bested me again.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: match key to formula

Post 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.
Post Reply