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
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
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
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
Share on other sites

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

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.