Results 1 to 10 of 10

Thread: explode

  1. #1
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    1,696
    Thanks
    82
    Thanked 90 Times in 88 Posts

    Default explode

    Let's say I have the address:

    mysite.com/display.php?category=php+css

    Code:
    <?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
    To choose the lesser of two evils is still to choose evil. My personal site

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,162
    Thanks
    263
    Thanked 690 Times in 678 Posts

    Default

    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 )
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    1,696
    Thanks
    82
    Thanked 90 Times in 88 Posts

    Default

    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.
    Last edited by james438; 03-21-2012 at 05:23 AM.
    To choose the lesser of two evils is still to choose evil. My personal site

  4. #4
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 517 Times in 503 Posts
    Blog Entries
    5

    Default

    you could parse the query string yourself:
    PHP Code:
    <?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

  5. #5
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    1,696
    Thanks
    82
    Thanked 90 Times in 88 Posts

    Default

    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.

    Code:
    $category=$_SERVER['QUERY_STRING'];
    $category=str_replace("category=","",$category);
    $category=explode('+',$category);
    print_r($category);
    To choose the lesser of two evils is still to choose evil. My personal site

  6. #6
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 517 Times in 503 Posts
    Blog Entries
    5

    Default

    Cool - as long as category is the only var passed

    Edit:

    how about:
    PHP Code:
    $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

    Last edited by traq; 03-21-2012 at 02:21 PM.

  7. #7
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,162
    Thanks
    263
    Thanked 690 Times in 678 Posts

    Default

    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.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  8. #8
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 517 Times in 503 Posts
    Blog Entries
    5

    Default

    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.)

  9. #9
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    1,696
    Thanks
    82
    Thanked 90 Times in 88 Posts

    Default

    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.
    To choose the lesser of two evils is still to choose evil. My personal site

  10. #10
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,162
    Thanks
    263
    Thanked 690 Times in 678 Posts

    Default

    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.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •