0

I am a php newbie, and I'm trying to follow suggestions for how to get the query string. I want the words searched on in a readable format.

Now, the strings come from a database, where they have been previously collected using the request object's referer property in Asp.Net. But as far as I know that shouldn't make any difference.

So I tried this:

function getQuery($url)
{
        $processed_url = parse_url( $url );

        $query_string = $processed_url['query'];
        if($url != '')
        {
            return $query_string;
        }
        else
        {
            return $url;
        }
}

So I tried a variation, which should also extract the query string:

    $query = parse_url($url, 6);

    return $query;

Well, that sort of works. It gives me a query string part, but including the "q=" and all that, not just the text of the query string.

So I tried the parse_str() function on this, which is supposed to be able to parse the query string itself:

parse_str($query, $myArray);
return $myArray[0];

But that didn't work at all, it gives me no results in the result page (a grid).

What am I doing wrong? First of all with the first method for getting the query above, and secondly for parsing the query string into its components? (the 0 index was just an example, I thought I'd concatenate it later if I managed to get out only the text parts)?

hakre
  • 193,403
  • 52
  • 435
  • 836
Anders
  • 12,556
  • 24
  • 104
  • 151

1 Answers1

0

It would seem that you are trying to parse a url from a string, not from the current request (as mentioned in comment)

On the PHP docs in the comments, there is a great function, that does exactly what you want, i have copied it here, so it can be used in future references - all credit goes to "Simon D" - http://dk.php.net/manual/en/function.parse-url.php#104527

<?php 
/** 
 * Returns the url query as associative array 
 * 
 * @param    string    query 
 * @return    array    params 
 */ 
function convertUrlQuery($query) { 
    $queryParts = explode('&', $query); 

    $params = array(); 
    foreach ($queryParts as $param) { 
        $item = explode('=', $param); 
        $params[$item[0]] = $item[1]; 
    } 

    return $params; 
} 
?>

which means it should be used like this:

$array = convertUrlQuery(parse_url($url, 6));
Jan Dragsbaek
  • 8,078
  • 2
  • 26
  • 46
  • I don't understand, how can I do that with a url string that comes from a database (not the current request)? If I'm misunderstanding, please give me a usage example... – Anders Sep 29 '11 at 23:22
  • how can a url string come from the database? What are you working with? – Jan Dragsbaek Sep 29 '11 at 23:23
  • As I mentioned, they have been previously stored in the database, coming from the request object in Asp.Net. So what the current php application needs to do is to be able to read those urls in the database and parse them to readable format for display in a grid. – Anders Sep 29 '11 at 23:30
  • i updated the post with a solution after re-reading your post (its late and i misunderstood your initial question) – Jan Dragsbaek Sep 29 '11 at 23:31
  • @AndersSvensson I've updated my code. Is that what you want? Please clarify. – Mob Sep 29 '11 at 23:34
  • Well, sort of, it should be something like that, but I just tried that, and I still don't get the results. For simplicity I just tried printing the first index of the array into the grid, as $array[0], but it breaks the code. I obviously get an error somewhere, because the grid is suddenly completely empty (It's a JQ Grid, and being empty I suspect it is not getting the correct input). – Anders Sep 29 '11 at 23:37
  • It wouldn't work that way @AndersSvensson do it like this `echo $url['scheme'];` – Mob Sep 29 '11 at 23:42
  • @All: No, I actually don't think there is anything wrong with the simpler code, the one I already have in my question, because that works fine if I put in an example url manually, like this: $url = 'http://www.jobrapido.se/?w=teknikinformat%C3%B6r&l=malm%C3%B6&r=auto'; Then I get proper results. But I think the problem is somewhere in the fact that some of these records have empty fields for the url, and some may have urls that are problematic (malformed or something), some are extremely long and complex, coming from some Google adclick network. And I don't know how to handle that. – Anders Sep 29 '11 at 23:48