AtomicRax Posted July 10, 2007 Share Posted July 10, 2007 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... Quote Link to comment https://forums.phpfreaks.com/topic/59252-solved-preg_match/ Share on other sites More sharing options...
Wildbug Posted July 10, 2007 Share Posted July 10, 2007 <?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.) Quote Link to comment https://forums.phpfreaks.com/topic/59252-solved-preg_match/#findComment-294281 Share on other sites More sharing options...
AtomicRax Posted July 10, 2007 Author Share Posted July 10, 2007 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... Quote Link to comment https://forums.phpfreaks.com/topic/59252-solved-preg_match/#findComment-294284 Share on other sites More sharing options...
MadTechie Posted July 10, 2007 Share Posted July 10, 2007 Add to the end if (preg_match('/q=(.*?)&/i', $ref, $regs)) { $search= $regs[0]; } Quote Link to comment https://forums.phpfreaks.com/topic/59252-solved-preg_match/#findComment-294293 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.