phpretard Posted May 25, 2008 Share Posted May 25, 2008 This is what $_SERVER['HTTP_REFERER'] gets when some searches "tallahassee+web+designer" on google and clicks on my site. I am putting that info into a database. When I pull that info from the database how can I echo the "q=tallahassee+web+designer" only? http://www.google.com/search?hl=en&rls=com.microsoft:*&pwst=1&q=tallahassee+web+designer&start=10&sa=N Quote Link to comment https://forums.phpfreaks.com/topic/107227-_getgoogle/ Share on other sites More sharing options...
phpretard Posted May 25, 2008 Author Share Posted May 25, 2008 After reading the manual (as per the Terms of this site) $var="http://www.google.com/search?hl=en&rls=com.microsoft:*&pwst=1&q=tallahassee+web+designer&start=10&sa=N"; $me=substr_replace($var, 'everything between Q and the &'); is the idea. echo $me; I would like $me to =tallahassee+web+designer; Quote Link to comment https://forums.phpfreaks.com/topic/107227-_getgoogle/#findComment-549744 Share on other sites More sharing options...
sasa Posted May 25, 2008 Share Posted May 25, 2008 <?php $var="http://www.google.com/search?hl=en&rls=com.microsoft:*&pwst=1&q=tallahassee+web+designer&start=10&sa=N"; parse_str($var, $out); echo $out['q']; ?> Quote Link to comment https://forums.phpfreaks.com/topic/107227-_getgoogle/#findComment-549751 Share on other sites More sharing options...
phpretard Posted May 25, 2008 Author Share Posted May 25, 2008 Why would that work great for: http://www.google.com/search?hl=en&rls=com.microsoft:*&pwst=1&q=tallahassee+web+designer&start=10&sa=N But it doesn't work for this one: http://www.google.com/search?q=Tallahassee+Websites&rls=com.microsoft:*&ie=UTF-8&oe=UTF-8&startIndex=&startPage=1 Quote Link to comment https://forums.phpfreaks.com/topic/107227-_getgoogle/#findComment-549753 Share on other sites More sharing options...
BlueSkyIS Posted May 25, 2008 Share Posted May 25, 2008 you should only be using the query string, not the entire URL. void parse_str ( string str [, array &arr] ) Parses str as if it were the query string passed via a URL and sets variables in the current scope. If the second parameter arr is present, variables are stored in this variable as array elements instead. $var = 'q=Tallahassee+Websites&rls=com.microsoft:*&ie=UTF-8&oe=UTF-8&startIndex=&startPage=1'; parse_str($var, $out); echo $out['q']; Quote Link to comment https://forums.phpfreaks.com/topic/107227-_getgoogle/#findComment-549757 Share on other sites More sharing options...
thebadbad Posted May 25, 2008 Share Posted May 25, 2008 This should work then: <?php $var = 'http://www.google.com/search?hl=en&rls=com.microsoft:*&pwst=1&q=tallahassee+web+designer&start=10&sa=N'; list(, $q_str) = explode('?', $var); parse_str($q_str, $out); echo urlencode($out['q']); ?> The RegEx method: <?php $var = 'http://www.google.com/search?hl=en&rls=com.microsoft:*&pwst=1&q=tallahassee+web+designer&start=10&sa=N'; preg_match('%(?<=q=).*?((?=&)|$)%', $var, $match); echo $match[0]; ?> I'm using a positive lookbehind (?<=q=) meaning the following string .*? (the actual match) shall always be preceded by "q=", and an 'optional' positive lookahead ((?=&)|$), meaning the match should be followed by either an & or the "end-of-string" $. More info: http://www.regular-expressions.info/lookaround.html Quote Link to comment https://forums.phpfreaks.com/topic/107227-_getgoogle/#findComment-549763 Share on other sites More sharing options...
sasa Posted May 25, 2008 Share Posted May 25, 2008 <?php $var="http://www.google.com/search?q=Tallahassee+Websites&rls=com.microsoft:*&ie=UTF-8&oe=UTF-8&startIndex=&startPage=1"; $var = parse_url($var); parse_str($var['query'], $out); echo $out['q']; ?> Quote Link to comment https://forums.phpfreaks.com/topic/107227-_getgoogle/#findComment-549769 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.