Jump to content

Split Url


dlf1987

Recommended Posts

<?php
$refer = 'http://www.google.com/search?q=XBOX+360&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a';

// Google
if (preg_match("/\bgoogle/", "$refer")) {
$refersplit = SE_filter($refer);
list($SEQ1, $SEQ2) = split('q=', $refersplit);
$refersplit = SE_filter($SEQ2);
list($SEQ1) = split('&', $refersplit);
}

echo $SEQ1;
?>

 

The above code works and echo's $SEQ1 which is "XBOX 360", but if i have a url that looks like this...

 

$refer = 'http://www.google.com/search?sourceid=navclient&aq=t&q=XBOX+360';

 

Then it echo's  "t" because it pulls the first aq= instead of just q=

 

How do i fix that?

 

Hope this makes sense :D

Link to comment
Share on other sites

if you just want to grab q every time:

 

$refer = 'http://www.google.com/search?q=XBOX+360&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a';

// Google
if (preg_match("/\bgoogle/", "$refer")) {
$url_parts = parse_url($refer);
        $url_query = $url_parts['query'];
        $q_vals = parse_str($url_query);
}

echo $q;

Link to comment
Share on other sites

if you just want to grab q every time:

 

$refer = 'http://www.google.com/search?q=XBOX+360&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a';

// Google
if (preg_match("/\bgoogle/", "$refer")) {
$url_parts = parse_url($refer);
        $url_query = $url_parts['query'];
        $q_vals = parse_str($url_query);
}

echo $q;

 

 

That doesnt pull the q from $refer though...

 

Looks like you just updated your post

 

Link to comment
Share on other sites

yes. the parse_str() function loads all of the variables into memory. so every query key in the string will be in memory regardless of what it is. in our example, with the single parse_str() function, in addition to $q, you'll also have $ie, $oe, $aq, and all the rest.

Link to comment
Share on other sites

yes. the parse_str() function loads all of the variables into memory. so every query key in the string will be in memory regardless of what it is. in our example, with the single parse_str() function, in addition to $q, you'll also have $ie, $oe, $aq, and all the rest.

 

What i mean is, is it possible to say "echo $keyword", but display the values of q from the URL? Or did you just answer my question, and I'm not understanding?

Link to comment
Share on other sites

Ignore me. parse_str has had an array argument for a while now. Been a long time since I've looked at the function.

 

What i mean is, is it possible to say "echo $keyword", but display the values of q from the URL? Or did you just answer my question, and I'm not understanding?

 

Do this: [updated]

 

$refer = 'http://www.google.com/search?sourceid=navclient&aq=t&q=XBOX+360';

 

$url = parse_url($refer);

parse_str($url['query'], $queries);

$keyword = $queries['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.