Nils Luxton
Nils Luxton

Reputation: 766

Is there a PHP equivalent of Perl's URI::ParseSearchString?

I'm doing some work for a client that involves parsing the referrer information from Google et al to target various parts of a page to the user's search keywords.

I noticed that Perl's CPAN has a module called URI::ParseSearchString which seems to do exactly what I need. The problem is, I need to do it in PHP.

So, to avoid reinventing the wheel, does anyone know if there is a library out there for PHP that does the same / similar thing?

Upvotes: 1

Views: 298

Answers (3)

sdolgy
sdolgy

Reputation: 7001

Maybe this is too inefficient or the http_referer isn't showing the full uri ...

function parse_uri($uri) {
  if (substr_count('?', $uri) > 0) { 
    $queryString = explode('?', $uri);
    return parse_str($queryString[1]);
  } else { 
    return parse_str($uri);
  }
}


if (isset($_SERVER['HTTP_REFERER'])) { 
        print_r(parse_uri($_SERVER['HTTP_REFERER']));
}

Upvotes: 0

Spiros
Spiros

Reputation: 2281

I'm the author of the module. As far as I know, I've never seen something similar for PHP. If you do come across anything, please do let me know.

That being said, I cannot image this being very hard to port to PHP and I can have an attempt at it if you dont find anything similar out there.

Spiros

Upvotes: 0

soulmerge
soulmerge

Reputation: 75714

parse_str() is what you are looking for.

You may additionally want to use parse_url() to get the search string.

Upvotes: 3

Related Questions