Jump to content

$_GET['Google']


phpretard

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/107227-_getgoogle/
Share on other sites

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;


Link to comment
https://forums.phpfreaks.com/topic/107227-_getgoogle/#findComment-549744
Share on other sites

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'];

Link to comment
https://forums.phpfreaks.com/topic/107227-_getgoogle/#findComment-549757
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/107227-_getgoogle/#findComment-549763
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.