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
https://forums.phpfreaks.com/topic/94289-split-url/
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
https://forums.phpfreaks.com/topic/94289-split-url/#findComment-482963
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
https://forums.phpfreaks.com/topic/94289-split-url/#findComment-482969
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
https://forums.phpfreaks.com/topic/94289-split-url/#findComment-482986
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
https://forums.phpfreaks.com/topic/94289-split-url/#findComment-482989
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.