Stumped

Mike

New member
Jun 27, 2006
6,777
116
0
51
On the firing line
Real quick... using PHP preg_match to try to match portions of a keyword. If said portion matches, I want to assign a value to a variable.

Unfortunately, EVERY SINGLE TIME the variable returns the first value.

Here's a sample of my code:

Code:
switch ($kw){
    case preg_match("/\A-AB\z/",$kw):
        $tag1 = "49";
        break;
    case preg_match("/\A-BC\z/",$kw):
        $tag1 = "51";
        break;    
    case preg_match("/\A-DE\z/",$kw):
        $tag1 = "52";
        break;    
    case preg_match("/\A-FG\z/",$kw):
        $tag1 = "53";
        break;
    case preg_match("/\A-HI\z/",$kw):
        $tag1 = "50";
        break;
    case preg_match("/\A-JK\z/",$kw):
        $tag1 = "54";
        break;
    default:
        $tag1 = "56";
        break;    
}

Someone please help. PHP.net and Google have failed me so far.
 


I've never seen that expression.

Ive seen a-zA-Z to match all alpha, is yours saying to match a to ab ( b as end )?

If so, aren't you matching A on all of them ( including the first one )?
 
Your regex makes zero sense. You'd do better to show what you're actually trying to match. You'd also likely do better without a switch.
 
No idea what your regex is trying to do, but here is a better way to structure your code:

HTML:
$preg_array = array(
    '/regex1/' => '1',
    '/regex2/' => '2',
    );

$tag1 = 56; //default value
foreach($preg_array as $regex => $value)
{
    if(preg_match($regex, $kw))
    {
        $tag1 = $value;
    }
}
 
Your switch is all fucked up.

You're basically saying,
If your regex matches:
if the first case is true, then $tag1=49

But if the regex doesn't match:
if the first case is false, then $tag1=49

So your switch will always take the first case.
and the first regex is trying to match string literal "-AB"
 
Regex is not my strong suit. It's like trying to write Japanese to me. So, I was using examples from PHP.net and hoping it was right.

Basically, I want to match a portion of the URL query string.

Let's say it's:

Code:
http://somesite.com/?a=123&b=345&c=AB932&keyword=123-345-AB

I want the preg_match to look at the query string and say "I found -AB" and because that's true, I want to assign a value to a new variable.

Switches also mess me up. I do better with if then statements, but was using a switch because I read it's more efficient than if then's.

PHP: preg_match - Manual about 1/3 of the way down the page is a regex reference that I used for the code. According to the reference "\A" is the Start of string and "\z" is the End of string.
 
Try this, I think that is what you want:

Code:
$preg_array = array(
    '/\-AB/' => '49',
    '/\-BC/' => '51',
    '/\-DE/' => '52',
    '/\-FG/' => '53',
    '/\-HI/' => '50',
    '/\-JK/' => '54',
    );

$tag1 = '56'; //default value
foreach($preg_array as $regex => $value)
{
    if(preg_match($regex, $kw))
    {
        $tag1 = $value;
    }
}
 
That is quite possibly the dumbest comment you've ever made.

you should be writing switch case statements in a way where the cascading boolean conditions will automatically break out of the case upon success (IE they should be ordered in a way that once one case is true, the rest of the statements thereafter can't be true).

inb4 you write a case statement in php and start spitting off about how fast it runs
 
also, just discovered you have to implicitly break each case in PHP. That's the dumbest feature I've ever fucking seen. so ignore me, I'll just go back to my appropriately non-verbose languages.

EDIT: WAIT A FUCKING MINUTE

<?php
switch ($i) {
case
0:
echo
"i equals 0";
case
1:
echo
"i equals 1";
case
2:
echo
"i equals 2";
}
?>

http://php.net/manual/en/control-structures.switch.php

What I wrote here: http://www.wickedfire.com/design-development-programming/159212-stumped.html#post1779097 is correct per PHP docs.

suck_it_img1.gif
 
Code:
http://somesite.com/?a=123&b=345&c=AB932&keyword=123-345-AB
I want the preg_match to look at the query string and say "I found -AB" and because that's true, I want to assign a value to a new variable.

Switches also mess me up. I do better with if then statements, but was using a switch because I read it's more efficient than if then's.

PHP: preg_match - Manual about 1/3 of the way down the page is a regex reference that I used for the code. According to the reference "\A" is the Start of string and "\z" is the End of string.

Why the fuck use regex at all?

Code:
if(stristr($_GET['keyword'], '-AB'){ //or use strstr if it needs to be case sesitive
//do something
} elseif(stristr($_GET['keyword'], '-CD'){
} etc...

Way more effective, way less of a pain in the ass.
 
Why the fuck use regex at all?

Code:
if(stristr($_GET['keyword'], '-AB'){ //or use strstr if it needs to be case sesitive
//do something
} elseif(stristr($_GET['keyword'], '-CD'){
} etc...

Way more effective, way less of a pain in the ass.

please don't try to mask your ignorance with arrogance
 
also, just discovered you have to implicitly break each case in PHP. That's the dumbest feature I've ever fucking seen. so ignore me, I'll just go back to my appropriately non-verbose languages.

EDIT: WAIT A FUCKING MINUTE



PHP: switch - Manual

What I wrote here: http://www.wickedfire.com/design-development-programming/159212-stumped.html#post1779097 is correct per PHP docs.

The fuck nigga, can't you read?

RIGHT FUCKING ABOVE YOUR EXAMPLE:

"If you don't write a break statement at the end of a case's statement list, PHP will go on executing the statements of the following case."

And the reason this is useful for non ruby fags is:

switch ($i) {
case 0:
case 1:
case 2:
echo "i is less than 3 but not negative";
break;
case 3:
echo "i is 3";
}
 
The fuck nigga, can't you read?

you should be writing switch case statements in a way where the cascading boolean conditions will automatically break out of the case upon success (IE they should be ordered in a way that once one case is true, the rest of the statements thereafter can't be true).
 
Can we just all agree that switch is not the optimal solution in this case?

Everything else won't help OP much... I can't even remember the last time I used switch anyway.

Ditto, case is the most worthless statement. If, then, else, solved.
 
The fuck nigga, can't you read?

And your statement is still wrong. Case statements do not and should not re-check their case after a match.

[root@boost ~]# cat asd.php
<?
$dchuck = 'sped';

switch ($dchuck)
{
case 'moron':
echo "true\n";
case 'sped':
echo "truer\n";
case 'fag ruby coder':
echo "truest\n";
}
[root@boost ~]# php asd.php
truer
truest


Ditto, case is the most worthless statement. If, then, else, solved.

Simply not the case. There are tons of places where case statements make sense. Not to mention they are often easier to read than a long set of if/then/else statements.