Announcement

Collapse
No announcement yet.

prevent " "s from explode()ing

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • prevent " "s from explode()ing

    Is there any way to not have stuff between quotes explode, or to recombine everything between quotes afterwards?

  • #2
    What do you mean? In what circumstance does something between quotes 'automatically' explode? o_O

    Comment


    • #3
      He uses explode() on a string and doesn't want text between quotes to be processed by explode()...

      Maybe strip the quoted text and add it after you used explode() ? Or you have to create your own function, explode() alone can't do it.

      Comment


      • #4
        Yeah but I wonder what he is exploding on, cos explode() itself doesn't do anything with " unless you specify it to, but then you're being silly

        Comment


        • #5
          Not with quotes, with the text between quotes.

          Comment


          • #6
            PHP Code:
            $query '"foo" one two "three four" five "six"';
            // You should process $query a bit here, for example trimming and contracting multiple spaces into one
            $queryBits explode('"'$query);
            $result = array();
            if (
            $query{0} == '"') {
            do {
            $first array_shift($queryBits);
            } while (
            trim($first) == '');
            $result[] = $first;
            }
            for (
            $i 0$i count($queryBits); $i++) {
            if ((
            $queryBits[$i] = trim($queryBits[$i])) == '') {
            continue;
            }
            if (
            $i == 0) {
            $result array_merge($resultexplode(' '$queryBits[$i]));
            } else {
            $result[] = $queryBits[$i];
            }
            }
            print_r($result); 
            Output:
            Code:
            Array
            (
            [0] => one
            [1] => two
            [2] => three four
            [3] => five
            [4] => six
            )
            Please note I have taken quite a few shortcuts so if you want the code to be 100% reliable you will need to play with it a little. Also, there are countless other methods for doing the same thing and this may not be the fastest way. Someone with good PCRE knowledge migth be able to pull this quickly with preg_split().

            (WYSIWYG editor ruined my tabs. )
            Last edited by Chen; Fri 8th Aug '03, 6:15am.
            Chen Avinadav
            Better to remain silent and be thought a fool than to speak out and remove all doubt.

            גם אני מאוכזב מסיקור תחרות לתור מוטור של NRG הרשת ע"י מעריב

            Comment


            • #7
              Here's another solution (prettier ):
              PHP Code:
              define('MD5MICRO'md5(microtime()));
              function 
              hide_spaces(&$string$key$modvalue) {
               if (
              $key == (int) $modvalue) {
                
              $string str_replace(' 'MD5MICRO$string);
               }
              }
              function 
              show_spaces(&$string$key) {
               
              $string str_replace(MD5MICRO' 'trim($string));
              }
              $query '"foo" one two "three four" five "six"';
              $queryBits explode('"'$query);
              array_walk($queryBits'hide_spaces'$query{0} == '"');
              $queryBits explode(' 'implode(' '$queryBits));
              array_walk($queryBits'show_spaces');
              $queryBits array_filter($queryBits'strlen');
              print_r($queryBits); 
              Chen Avinadav
              Better to remain silent and be thought a fool than to speak out and remove all doubt.

              גם אני מאוכזב מסיקור תחרות לתור מוטור של NRG הרשת ע"י מעריב

              Comment

              widgetinstance 262 (Related Topics) skipped due to lack of content & hide_module_if_empty option.
              Working...
              X