Page 1 of 1

split string into chunks

Posted: Sun May 27, 2007 8:50 pm
by yanisdon
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hello,
Hope you can get me going with this:

I've got the following URL:

Code: Select all

http://mysite.com/abc/xsl/search?q=dc.date.lastmodified:%5b2007-05-01T00:00:00Z%20TO%202007-05-19T00:00:00Z%5d&user=username&sr=resource&type=rss
Now, I'd like to split that URL's query string part into chunks.

Code: Select all

parse_url gives me sopmething like this:
    [scheme] => http
    [host] => mysite.com
    [path] => /abc/xsl/search
    [query] => q=dc.date.lastmodified:%5b2007-05-01T00:00:00Z%20TO%202007-05-19T00:00:00Z%5d&user=username&sr=resource&type=rss
to get the 'query'-part I could also use :

Code: Select all

$url = 'http://mysite.com/abc/xsl/search?q=dc.date.lastmodified:%5b2007-05-01T00:00:00Z%20TO%202007-05-19T00:00:00Z%5d&user=username&sr=resource&type=rss';

    $q_str = parse_url($url, PHP_URL_QUERY);
     print $q_str;

// that would give me something like this:
// q=dc.date.lastmodified:%5b2007-05-01T00:00:00Z%20TO%202007-05-19T00:00:00Z%5d&user=username&sr=resource&type=rss
Any ideas.

Cheers
Jan


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Sun May 27, 2007 9:51 pm
by superdezign

Posted: Sun May 27, 2007 10:44 pm
by Kieran Huggins
explode() is a lighter-weight alternative

Posted: Mon May 28, 2007 1:25 am
by yanisdon
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Thanks for that.



I get that already with [b]parse_url[/b]:

Code: Select all

//So I get all desired values using something like this:

 $q_str = parse_url(REQUEST_URL, PHP_URL_QUERY);      // get the query string
  // print $q_str;
 parse_str(html_entity_decode($q_str), $output);		  // split the query string into chunks
 
 // result:
 // Array
 // (
 //   [q] => dc.date.lastmodified:[2007-05-01T00:00:00Z TO 2007-05-19T00]
 //   [user] => username
 //   [sr] => resource
 //   [type] => rss
 // )
I was actually trying to split the query part which is this:

Code: Select all

q=dc.date.lastmodified:%5b2007-05-01T00:00:00Z%20TO%202007-05-19T00:00:00Z%5d


Cheers
Jan


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Mon May 28, 2007 3:35 am
by Kieran Huggins
It seems you have an easy time posting, but not such an easy time reading.

Please use proper

Code: Select all

,

Code: Select all

and [syntax] tags in your future posts.

As for parsing the query string (which we all understood, by the way) you can use explode() to split it at certain strings (like & or =) or you can use split() or preg_split() if you want to use a regular expression.

If all you want is an array of the keys and values passed, look at the $_GET superglobal.