View Full Version : explode
james438
03-20-2012, 05:27 PM
Let's say I have the address:
mysite.com/display.php?category=php+css
<?php
$category=$_GET['category'];
$category=explode("+",$category);
print_r($category);
?>
The above does not work because the plus sign appears to be recognized as something else since it came from the address bar. What should I do to recognize the plus sign?
P.S. Yay! 1000th Post :)!
and it only took a little over 5 years to accomplish :p
djr33
03-20-2012, 09:08 PM
I don't think you can do that. You could use a URL-encoded + sign: %2B. Of course you could also use str_replace to consider all spaces to be +, but that would overgeneralize.
(Congrats on 1000-- almost-- looks like you're still 1 post away. I'm nearing that with 10,000 too. I've been here forever, and I talk too much though :p)
james438
03-20-2012, 11:39 PM
Doh... It looks like one of my earlier posts must have been deleted.
I'll have to try something else then.
Edit: What I meant was that I'll have to try something else besides using the plus sign.
you could parse the query string yourself:
<?php
// mysite.com/display.php?category=php+css
// $QS will hold the query string, as originally written
$QS = $_SERVER['QUERY_STRING'];
// break it into individual key=value chunks
$xQS = strpos( $QS,'&' )? explode( '&',$QS ): array( $QS );
foreach( $xQS as $qs ){
// if there is a key=value pair, break it into an array
if( strpos( $qs,'=' ) ){
$xqs = explode( '=',$qs );
$myGET[ $xqs[0] ] = $xqs[1];
}
// you could also "overwrite" the actual $_GET array if you wanted,
// but I wouldn't.
}
// then use your original idea
if( !empty( $myGET['category'] ) && strpos( $myGET['category'],'+' ) ){
$category = explode( '+',$myGET['category'] );
var_dump( $category );
// prints " array(2) { [0]=> string(3) "php" [1]=> string(3) "css" } "
}up to you to decide if it's worth the effort, I guess :p
james438
03-21-2012, 01:15 PM
Excellent! I don't know why using $_SERVER didn't occur to me. What you suggested is a little bit more complicated for what I am trying to do right now, so I am just using something similar to this instead.
$category=$_SERVER['QUERY_STRING'];
$category=str_replace("category=","",$category);
$category=explode('+',$category);
print_r($category);
Cool - as long as category is the only var passed :)
how about:
$QS = $_SERVER['QUERY_STRING'];
if( preg_match_all( '#category=([^&]+)#ui',$QS,$matches,PREG_SET_ORDER ) ){
$cat = $matches[0][1];
$category = strpos( $cat,'+' )? explode( '+',$cat ): array( $cat );
var_dump( $category );
// prints " array(2) { [0]=> string(3) "php" [1]=> string(3) "css" } "
}no errors
djr33
03-21-2012, 11:08 PM
Hmm... does that work well, though? Won't some browsers/servers automatically convert + to space without telling you? I'm not sure I'd rely on it.
grumblegrumble... :: pulls out IE ::
surprisingly, don't seem to. works as expected on:
IE7
IE8
Firefox 3.6+
Chrome
Safari (Win)
Opera
Now, the opposite (automatically converting spaces to + or %20whatever) happens fairly often - but [+] is a valid character for URLs, so I don't think it's too big a risk.
Servers, I don't know about - but Apache, at least, doesn't seem to mess with the query strings. Obviously, PHP has access to the original text as entered by the user. (I'm using Apache 2.2.14 on both my Linux and Windows boxes.)
james438
03-22-2012, 04:50 AM
I just tested it in the latest versions of Opera, Firefox, Chrome, and IE8 and it works. Glad you mentioned that though.
Thanks for the suggestions traq, but not really the best fit for my script. "category" is the only variable passed.
The script is just a simple sorting script. At present it lists all of the categories and when the user clicks on a category then it lists all of the articles in that category as linked titles. Click on a title and read the article. What I am trying to do now is add the ability to narrow the results by multiple categories such as articles that have both the tags "php" and "css" or whatever. Beyond that there is little need to expand the script further.
djr33
03-22-2012, 06:05 AM
Thanks for checking. That's good to know.
Now, the opposite (automatically converting spaces to + or %20whatever) happens fairly often - but [+] is a valid character for URLs, so I don't think it's too big a risk.That's what I was thinking about mostly. So be warned, James, you can only rely on this if you are certain that you won't also want to use spaces (in a distinct way from the + sign). If you don't care, if it doesn't matter if spaces are converted to +, then you are fine, I think.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.