I'm having a hell of a time getting a nice regular expression to work with preg_split. I very clearly have no idea what I'm doing when it comes to regex's.

What I want to do is find quoted strings in a search box and then have any other strings become other array entries.

So the following string:
start test "test string 1" "test string 2" not a test
would become
ar[0]="start"
ar[1]="test"
ar[2]="test string 1"
ar[3]="test string 2"
ar[4]="not"
ar[5]="a"
ar[6]="test"
I've tried hundreds of things that didn't work, so I might as well not give what I've already tried, because it's not going to get us anywhere.

What I CAN say is that this expression, in expresso produces close to the right results: "["\r\n]"
It would show me anything in quotes. But it wouldn't bring back the extra. However, when I put /"["\r\n]
"/ into preg_split, it absolutely doesn't work.

Any thoughts?

    if you dont care about the order

    $str='start test "test string 1" "test string 2" not a test';
    preg_match_all('#\".*?\"#',$str,$foo);
    $str2=preg_replace('#\".*?\"#','',$str);
    $str3=preg_split("/[\s]+/",$str2);
    
    //print_r($foo);
    //echo $str2;
    //print_r($str3);
    
    $str4=array_merge_recursive($foo['0'],$str3);
    
    print_r($str4);
    

    result:

    Array
    (
    [0] => "test string 1"
    [1] => "test string 2"
    [2] => start
    [3] => test
    [4] => not
    [5] => a
    [6] => test
    )

      Alternatively:

      $str = 'start test "test string 1" "test string 2" not a test';
      $arr = preg_split('#(?:[\'"]([^\'"]+)[\'"]|\s+)#', $str, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
      foreach ($arr as &$val) {
          $val = '"' . $val . '"';
      }
      echo '<pre>'.print_r($arr, true);
      
        nrg_alpha;10927262 wrote:

        Alternatively:

        much better than mine, i suck at regular expressions.

          Thanks 🙂

          Like anything else.. it takes practice, is all.

            And if you do...

            $str = 'start test "test string 1" "test string 2" not a test';
            
            $pieces = explode('"', $str);
            $inside_quotes = false;
            $entries = array();
            foreach($pieces as $piece)
            {
                if($inside_quotes)
            		$entries[] = array($piece);
            	else
            		$entries[] = preg_split('/\s/', $piece, -1, PREG_SPLIT_NO_EMPTY);
                $inside_quotes = !$inside_quotes;
            }
            $entries = call_user_func_array('array_merge', $entries);
            
              nrg_alpha;10927265 wrote:

              Thanks 🙂

              Like anything else.. it takes practice, is all.

              how much?? I've been writing php for 10+ years and i still suck at regular expressions. :eek:

                Thanks guys! It totally works and you totally RULE! Totally.

                  Weedpacket;10927266 wrote:

                  And if you do...

                  $str = 'start test "test string 1" "test string 2" not a test';
                  
                  $pieces = explode('"', $str);
                  $inside_quotes = false;
                  $entries = array();
                  foreach($pieces as $piece)
                  {
                      if($inside_quotes)
                  		$entries[] = array($piece);
                  	else
                  		$entries[] = preg_split('/\s/', $piece, -1, PREG_SPLIT_NO_EMPTY);
                      $inside_quotes = !$inside_quotes;
                  }
                  $entries = call_user_func_array('array_merge', $entries);
                  

                  Hmmm... plenty of song and dance it looks like. Beyond preg_split, I am seeing the use of explode and array_merge. Granted, I never benchmarked the solutions presented here (one thing I have learned is that more code ! necessarily = slower).

                  The end result (array entries) doesn't accomplish the desired end result as per the OP's request.

                  Found two things stand out that I didn't know about however.. the call_user_func_array()
                  And setting something from false to true via: $inside_quotes = !$inside_quotes; format. Never thought of it that way.. interesting.

                    dagon;10927267 wrote:

                    how much?? I've been writing php for 10+ years and i still suck at regular expressions. :eek:

                    I suppose that's a relative issue. I have only been using php for about 3 years (so long ways to go for sure). But I have focused plenty of energy on regex (I find it more like an addictive puzzle game more than anything else). Learning for me includes buying this book as well as help from other fellow forum members across sites.

                    So obviously, the more time you spend focusing on learning something, the faster you advance in it.

                    I'm weak in my own areas to be sure. It just depends on how much practice you get in I suppose.

                      nrg_alpha wrote:

                      The end result (array entries) doesn't accomplish the desired end result as per the OP's request.

                      If by that you mean quote marks around all of the elements, I thought that was a cosmetic issue, not a parsing one (what point would there be in adding quotes to the matched results at this stage?) - that's not a print_r() dump in the OP's post.

                        Well, if it's cosmetic, the OP shouldn't do that, as someone (like myself perhaps) might think that's the literal end result they seek.

                          Write a Reply...