IF statement - How do I use a wildcard?

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

User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

stripos will be even quicker. (php5)

Code: Select all

if (stripos('google', $var) !== false) {
  //'google' is in $var
}
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

The problem with that is you could have a url like this:

Code: Select all

http://www.site.com/review.php?ofSite=google
That's why the regex I posted is better.
I didn't test it though, probably should have, I rarely get regexes right first time ¬_¬
User avatar
fatman
Forum Commoner
Posts: 47
Joined: Fri Jul 21, 2006 11:18 am
Location: Portugal

Thanks for the ton of advice

Post by fatman »

My server just went PHP5, so stripos is interesting. I will try that as soon as I get back to the office.

Point taken with interest Ole, how does the regex you posted avoid me from running into a problem with the URL you show?
(Had to fiddle with it, my server didn't like it)
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Well it checks that google appears directly after http:// or http://www. not just anywhere in the string.
User avatar
fatman
Forum Commoner
Posts: 47
Joined: Fri Jul 21, 2006 11:18 am
Location: Portugal

Aaaah!

Post by fatman »

Sometimes, if I look, I will see.

Yes, thats perfect
Post Reply