Forum Moderators: coopster

Message Too Old, No Replies

Simple pregmatch in URI

Help

         

jimmywhite

10:39 am on May 23, 2010 (gmt 0)

10+ Year Member



Hello All, this is my first post on the forum. I'm a PHP newbie and i ran into a problem with a pregmatch function:
I want to search in a URI like http://example.com/cats-dogs-mice-snakes.html
I would like to search in that URI a string that includes 'cats' and 'snakes'.
I have tried the following, but it didn't not work:
<?php if (preg_match('#cats#|#snakes#', $_SERVER['REQUEST_URI'])) { ?>
echo my statement bla bla
<?php }?>
It's working if i am searching for only one word 'cats'
<?php if (preg_match('#cats#', $_SERVER['REQUEST_URI'])) { ?>
echo my statement bla bla
<?php }?>

Can anyone point me in the right direction?

Readie

11:26 am on May 23, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try the following:

<?php if (preg_match('/(cats|snakes)/i', $_SERVER['REQUEST_URI'])) { ?>

jimmywhite

12:16 pm on May 23, 2010 (gmt 0)

10+ Year Member



Readie, Thanks a lot for the quick reply. I would need my pregmatch to return true only if both of the words 'cats', 'snakes' match the URI. For now, if only one of those words match, the function returns true. I have tried with pregmatch_all instead of pregmatch, but then it returns false always. Perhaps pregmatch is not the function that does what i need?

Readie

12:50 pm on May 23, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ahh, I see. Try one of the following two then:

cats is always before snakes:
/^.*?cats.*?snakes.*?$/i

cats and snakes can be in any order:
/^.*?(cats)?(?(1)snakes|.?).*?(?(1)snakes|cats).*?$/i

I'm not 100% certain on the syntax of the second one, I've not really used if/else statements in regex before, so let me know if that one works if you decide to use it, please.

For the record, preg_match stops matching after the first pattern match. preg_match_all keeps on going, looking for more instances of the pattern which is why you had no joy there.

jimmywhite

1:13 pm on May 23, 2010 (gmt 0)

10+ Year Member



I have tried both of them, the first one works and it's exactly what i was looking for, it solved my problem (thanks a lot). The second one must have a wrong syntax because it broke my website when i tried it.
Anyway, you have been a great help, i like this forum already!
Cheers!

Readie

1:29 pm on May 23, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You're welcome :)

I'm annoyed the second one didn't work... Most of the alternatives I can think of have some sort of logical error... :/

I suppose for the second one I'd need to dip into PHP again, and do it like this:
<?php

if(preg_match('/.*?(cats|snakes).*?(cats|snakes).*?/i', $_SERVER['REQUEST_URI'], $out)) {
if(strtolower($out[1]) === strtolower($out[2])) {
// Error
} else {
// Success
}
}

?>
Problem with that is if you have something like /cats-cats-snakes.php it will fail.

Readie

3:09 pm on May 23, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



/^.*?(cats.*?snakes|snakes.*?cats).*?$/i

Can't think of a logic flaw with that one :)

Now that'll stop bugging me.

Alcoholico

5:25 pm on May 23, 2010 (gmt 0)

10+ Year Member



Wouldn't it be easier to use stripos instead of preg_match?, like this:
<?php
if(stripos($_SERVER['REQUEST_URI'], 'cats') !== false && stripos($_SERVER['REQUEST_URI'], 'snakes') !== false){
// TRUE
}
else {
//FALSE
}
?>

cats does not need to be before snakes, and it does not use the regex engine so it could be even more efficient.

jimmywhite

3:18 am on May 24, 2010 (gmt 0)

10+ Year Member



Readie, I wish i can help with some of that, but my PHP skills just won't allow me :)
Alcoholico, thanks a lot for that, i was thinking about using stripos, but didn't know the syntax, I am using it now as per your suggestion and it seems faster, thanks guys!

Readie

12:32 pm on May 24, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



i was thinking about using stripos, but didn't know the syntax

[php.net...] is your friend - use the function search, it's a brilliant resource.