Preg_split question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pascalito
    New Member
    • Oct 2009
    • 9

    Preg_split question

    Hi,

    i have a problem with a regular expression.

    i want to split a string on character pipe | with preg_split php function.

    The string is :

    "foo|bar|'hello |world\'abc\'de f'|bye"

    Result of split must be in an array :

    foo
    bar
    hello|world'abc 'def
    bye


    Anyone have an idea ?
    Thank you !!
    bye
    Last edited by pascalito; Oct 6 '09, 02:11 PM. Reason: adding preg_split function
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    What is the expression you're using?

    Comment

    • pascalito
      New Member
      • Oct 2009
      • 9

      #3
      Originally posted by Markus
      What is the expression you're using?
      i use preg_split function like this :

      Code:
      $val = "foo|bar|'hello|world\'abc\'def'|bye";
      $val = preg_split('/(?:\s\|\s)(?![\w\s]*\')/', $val, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
      print_r($val);
      But the pattern is wrong...
      Last edited by Dormilich; Oct 6 '09, 02:33 PM. Reason: Please use [code] tags when posting code

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        Hmm, the delimiter of your string is simply '|', that is, a pipe character? If so, explode() would be much better suited to it.

        Code:
        $string = "mark|jon|james|sarah|rachel|ben";
        
        print_r(explode('|', $string));

        Comment

        • pascalito
          New Member
          • Oct 2009
          • 9

          #5
          Originally posted by Markus
          Hmm, the delimiter of your string is simply '|', that is, a pipe character? If so, explode() would be much better suited to it.

          Code:
          $string = "mark|jon|james|sarah|rachel|ben";
          
          print_r(explode('|', $string));
          No, watch my example :

          "foo|bar|'hello |world\'abc\'de f'|bye"

          With explode function, i obtains :

          foo
          bar
          'hello
          world\'abc\'def '
          |bye

          I would like :

          foo
          bar
          hello|world'abc 'def
          bye

          It's not the same...
          Thank you

          Comment

          • TheServant
            Recognized Expert Top Contributor
            • Feb 2008
            • 1168

            #6
            I can't imagine what your string is actually about. Why is hello|world'abc 'def surrounded by inverted commas?? You would also like a pipe character | in one of the lines (not deliminated)?
            I have a feeling that you will want to use regex functions rather than preg_split or explode(), and explicitly state the exact pattern...

            It would be much easier to change your initial input to make the real deliminator something not included in one of your lines, or even a pattern like ||:
            Code:
            print_r(explode("||","foo||bar||'hello|world\'abc\'def'||bye"));
            Oh, and for the \'s in the exploded version, you might try stripslashes on it to get rid of them.

            Comment

            • pascalito
              New Member
              • Oct 2009
              • 9

              #7
              Originally posted by TheServant
              I can't imagine what your string is actually about. Why is hello|world'abc 'def surrounded by inverted commas?? You would also like a pipe character | in one of the lines (not deliminated)?
              I have a feeling that you will want to use regex functions rather than preg_split or explode(), and explicitly state the exact pattern...

              It would be much easier to change your initial input to make the real deliminator something not included in one of your lines, or even a pattern like ||:
              Code:
              print_r(explode("||","foo||bar||'hello|world\'abc\'def'||bye"));
              Oh, and for the \'s in the exploded version, you might try stripslashes on it to get rid of them.
              Your example is not good :

              If my string is "foo|bar|'hello ||world\'abc\'d ef'|bye" (note the ||)

              your example code return :

              Code:
              Array
              (
                  [0] => foo
                  [1] => bar
                  [2] => 'hello
                  [3] => world\'abc\'def'
                  [4] => bye
              )
              « 'hello||world\' abc\'def' » is splitted in 2 parts... not good

              I have found a solution with preg :

              Code:
              $val = preg_split("/([a-zA-Z0-9]+:'.+?'|[^\|]+)\||$/", $val, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
              Bye

              Comment

              • TheServant
                Recognized Expert Top Contributor
                • Feb 2008
                • 1168

                #8
                Glad you got it solved, but you did not read what I wrote. Between hello|world there is only one | while where you want a new line you use two ||.

                You said: "foo|bar|'hello ||world\'abc\'d ef'|bye"
                I said: "foo||bar||'hel lo|world\'abc\' def'||bye"

                Do you see the difference? I still think that as Markus suggested, it would be quicker and easier to use explode(), but as long as it works, I guess you're happy.

                Comment

                • pascalito
                  New Member
                  • Oct 2009
                  • 9

                  #9
                  Originally posted by TheServant
                  Glad you got it solved, but you did not read what I wrote. Between hello|world there is only one | while where you want a new line you use two ||.

                  You said: "foo|bar|'hello ||world\'abc\'d ef'|bye"
                  I said: "foo||bar||'hel lo|world\'abc\' def'||bye"

                  Do you see the difference? I still think that as Markus suggested, it would be quicker and easier to use explode(), but as long as it works, I guess you're happy.
                  quicker and easier ??
                  ok, write me the function who parse input data...

                  Comment

                  • pascalito
                    New Member
                    • Oct 2009
                    • 9

                    #10
                    there is a mistake in the preg_split solution, pattern is "/('.+?'|[^\|]+)\||$/"

                    Comment

                    • TheServant
                      Recognized Expert Top Contributor
                      • Feb 2008
                      • 1168

                      #11
                      Originally posted by pascalito
                      quicker and easier ??
                      ok, write me the function who parse input data...
                      Are you controlling your deliminators or are they changing??
                      I wrote a code:
                      Code:
                      print_r(explode("||","foo||bar||'hello|world\'abc\'def'||bye"));
                      
                      /* [U]Outputs[/U]: */
                      Array 
                      ( 
                          [0] => foo 
                          [1] => bar 
                          [2] => 'hello|world\'abc\'def' 
                          [3] => bye 
                      )
                      If you throw in a stripslashes:
                      Code:
                      print_r(explode("||",stripslashes("foo||bar||'hello|world\'abc\'def'||bye")));
                      
                      /* [U]Outputs[/U]: */
                      Array 
                      ( 
                          [0] => foo 
                          [1] => bar 
                          [2] => 'hello|world'abc'def' 
                          [3] => bye 
                      )
                      You have not shown us how your data is fetched/made, all you said was how do you turn "foo|bar|'hello |world\'abc\'de f'|bye" into and array of:
                      foo
                      bar
                      hello|world'abc 'def
                      bye
                      I suggested that if you change your deliminator from | to || then you can use explode while keeping your hello|world together.
                      If you deliminator is changing, you did not mention that. If you find preg_split easier, go for your life. Generally explode and similar string functions are easier for debugging.

                      Comment

                      • pascalito
                        New Member
                        • Oct 2009
                        • 9

                        #12
                        TheServant, you do not answer the question...

                        I know how to use the explode function, everyone knows how to use this function...

                        you say it is quicker and easier to convert input data, ok, I ask you to write the function that converts input data from "foo|bar|'hello |world\'abc\'de f'|bye" to "foo||bar||'hel lo|world\'abc\' def'||bye"

                        Regards,

                        Comment

                        • pascalito
                          New Member
                          • Oct 2009
                          • 9

                          #13
                          I do not control the delimitator, otherwise, I never asked and I simplified input string...

                          Comment

                          • TheServant
                            Recognized Expert Top Contributor
                            • Feb 2008
                            • 1168

                            #14
                            You never said that you couldn't control the deliminator, and how am I meant to know your experience? In that case, using regex (or preg_split) is the way you should go. Glad it's solved.

                            Comment

                            • pascalito
                              New Member
                              • Oct 2009
                              • 9

                              #15
                              Originally posted by TheServant
                              You never said that you couldn't control the deliminator, and how am I meant to know your experience? In that case, using regex (or preg_split) is the way you should go. Glad it's solved.
                              I never said any more than I could change the string but it seems logical to me.

                              If I could change the string, do you really think I asked any questions ...

                              Comment

                              Working...