Jump to content

[SOLVED] preg_match


AtomicRax

Recommended Posts

Ok, preg_match has me sooo confused...  ???

 

I am working on a program that logs where visitors are coming from... Such as a website or search engine... Now, I have the common URLs for the more common search engines... And I want to see if the url is contains the basic address, to know what search engine it was... I just don't know how to fill in the preg_match statement correctly... Actually, I've come back to this project after a few hours of not working on it, and am now lost in where I was going.... But here's what I had...

 

// $ref is the referring url
switch(preg_match('SOMETHING', $ref)) {
case "http://www.google.com/search":
$line = "Google";
$found = 1;
break;
case "http://search.msn.com/results.aspx":
$line = "MSN";
$found = 1;
break;
case "http://search.yahoo.com/search":
$line = "Yahoo";
$found = 1;
break;
case "http://search.live.com/results.aspx":
$line = "Windows Live";
$found = 1;
break;
}
// If no match
if($found != 1) {
echo "Not Found";
}

 

Like I said, no clue what I was thinking... but do you get the idea I have? Can you help? I'm lost...

Link to comment
Share on other sites

<?php

// $ref is the referring url
preg_match('|^http://(.*?)/|i', $ref, $match);
switch($match[1]) {
case "www.google.com":
	$line = "Google";
	break;
case "search.msn.com":
	$line = "MSN";
	break;
case "search.yahoo.com":
	$line = "Yahoo";
	break;
case "search.live.com":
	$line = "Windows Live";
	break;
default:
	echo "Not Found";
}

?>

 

 

 

(If you're storing these values in a database, you can also group and count by the first N characters of the string.)

Link to comment
Share on other sites

Thanks! That worked!

 

Now I have one more issue.... I have the entire URL, such as:

http://www.google.com/search?client=firefox-a&rls=org.mozilla%3Aen-US%3Aofficial&channel=s&hl=en&q=SEARCH_STRING&btnG=Google%20Search

 

What is the easiest way to just get what q equals (the string searched for to find the site)?

 

My way would be to split it at the & and then at the = and find the q and see what it equals... but there has to be an easier way...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.