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
fatman
Forum Commoner
Posts: 47
Joined: Fri Jul 21, 2006 11:18 am
Location: Portugal

IF statement - How do I use a wildcard?

Post by fatman »

I am trying to change the backgound color of the table cell where this info is stored, if the line returned from the DB is anything from Google. I was thinking using the wildcard (*) like in mySQL statements. The syntax is a problem though?

Code: Select all

<td <?php if ($row_myStats['referer'] == 'http://www.google.pt/search?q=alojamentos%2Beconomicos%2Balgarve&hl=pt-PT&lr=&start=10&sa=N') {echo "bgcolor=#FF0000";} ?>><?php echo $row_myStats['referer']; ?></td>
What I was thinking was something like this:

Code: Select all

*Google*
LiveFree
Forum Contributor
Posts: 258
Joined: Tue Dec 06, 2005 5:34 pm
Location: W-Town

Post by LiveFree »

Code: Select all

if (preg_match("#(.*?)Google(.*?)#i", $text)){
// You passed!
}
That will fire if the word Google is ANYWHERE in the $text var
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Code: Select all

if (preg_match('/^(http://)(www\.)?.*(google\.).*/i', trim($text))){
// You passed!
}
might be better.
User avatar
fatman
Forum Commoner
Posts: 47
Joined: Fri Jul 21, 2006 11:18 am
Location: Portugal

Stunning, Thanks

Post by fatman »

Works like a charm. I see exactly what you did, but I would never have found that myself

Thanks
User avatar
fatman
Forum Commoner
Posts: 47
Joined: Fri Jul 21, 2006 11:18 am
Location: Portugal

Now I want to use it in a mySQL statement

Post by fatman »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Now I want to use it in a mySQL statement

I want to count the records in this query. I tried this, but it doesn't work?

Code: Select all

$google = preg_match("#(.*?)Google(.*?)#i", $row_myStats['referer']);
mysql_select_db($database_sunrise, $sunrise);
$query_google = "SELECT referer FROM myStats WHERE referer = '$google'";
$google = mysql_query($query_google, $sunrise) or die(mysql_error());
$row_google = mysql_fetch_assoc($google);
$totalRows_google = mysql_num_rows($google);
Then I tried to show it like this:

Code: Select all

<?php echo $totalRows_myStats_counter ?>
Like I said, doesn't work


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I'm failing to see where $totalRows_myStats_counter is created.
User avatar
fatman
Forum Commoner
Posts: 47
Joined: Fri Jul 21, 2006 11:18 am
Location: Portugal

Sorry, yes

Post by fatman »

I'm inside my own head, so I see what I'm thinking ....


It looks like this:

Code: Select all

<?php
mysql_select_db($database_sunrise, $sunrise);
$query_myStats = "SELECT * FROM myStats WHERE `no` > 1 AND `host` <> '$ip_skip' AND host LIKE '$ip_show' ORDER BY `no` DESC";
$myStats = mysql_query($query_myStats, $sunrise) or die(mysql_error());
$row_myStats = mysql_fetch_assoc($myStats);
$totalRows_myStats = mysql_num_rows($myStats);

$google = preg_match("#(.*?)Google(.*?)#i", $row_myStats['referer']); // To define what we discussed

mysql_select_db($database_sunrise, $sunrise);
$query_google = "SELECT referer FROM myStats WHERE referer LIKE '$google'";
$google = mysql_query($query_google, $sunrise) or die(mysql_error());
$row_google = mysql_fetch_assoc($google);
$totalRows_google = mysql_num_rows($google);
?>
And then I want to show it

Code: Select all

<?php echo $totalRows_google ?>
Only now I see I posted this last piece of code wrong, sorry. I am trying to use it like this but no luck!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Are you sure you want to use the return value of preg_match()?

That's a hint, if you didn't get it.
User avatar
fatman
Forum Commoner
Posts: 47
Joined: Fri Jul 21, 2006 11:18 am
Location: Portugal

I'm a bit lost here

Post by fatman »

I have many lines in my DB that read something like this:

http://www.google.pt/search?q=alojament ... rt=10&sa=N

This is in the field: 'referer' and they all differ, obviously

I wanted to count how many lines contain the word google

After I thought about it:

STUPID STUPID STUPID STUPID I dont smack myself enough.

Code: Select all

$query_google = "SELECT referer FROM myStats WHERE referer = '%google%'";
Works like a charm. I wrote something like this 2 years ago.....
User avatar
fatman
Forum Commoner
Posts: 47
Joined: Fri Jul 21, 2006 11:18 am
Location: Portugal

Actually, NO!

Post by fatman »

Code: Select all

$query_google = "SELECT referer FROM myStats WHERE referer LIKE '%google%'";
Works
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post by nickvd »

Yes, but what would happen if the referrer was some how set to: "lafkjflkjf29083jr9w8j9wjgoogledf09s8ujsd09f8jdsa98djf" It should match, and that wouldnt be good...

Read up on preg_match and try doing a var_dump() or a print_r() on what it returns...
User avatar
fatman
Forum Commoner
Posts: 47
Joined: Fri Jul 21, 2006 11:18 am
Location: Portugal

So?

Post by fatman »

I'm afraid I dont know what you are trying to say
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Post by jmut »

LiveFree wrote:

Code: Select all

if (preg_match("#(.*?)Google(.*?)#i", $text)){
// You passed!
}
That will fire if the word Google is ANYWHERE in the $text var
BTW: Use this instead of such preg_match

Code: Select all

stristr()
Should be much faster
User avatar
fatman
Forum Commoner
Posts: 47
Joined: Fri Jul 21, 2006 11:18 am
Location: Portugal

OK?

Post by fatman »

Gotha!

What is the advantage of the one over the other?
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Re: OK?

Post by jmut »

fatman wrote:Gotha!

What is the advantage of the one over the other?
not sure if you were refereing to

Code: Select all

stristr()
but as I mentioned should be much faster. and my comment was just btw :)
Post Reply