Results 1 to 5 of 5

Thread: Regular Expression Help CSV

  1. #1
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default Regular Expression Help CSV

    Situation: Using a <textarea> a user can enter comma separated values(words). One value could be comprised of more than one word.

    I want whatever the user enters to be formatted as:

    var,var,var var, var var var,var,var var,var,var var var etc....

    No comma or spaces after the last value. No spaces before the first value. No spaces anywhere except when there is one value comprised of two or more words.

    The following code accomplishes this (I think). But there must be an easier way.

    Who is great at regular expressions and can explain this to me?

    Code:
    $var = rtrim(trim(str_replace(array("\t", "\r", "\n"), '', $var)), ',');
    
    $var = preg_replace('/\s{2,}/', " ", $var);
    
    $var = preg_replace('/,{2,}/', ",", $var);
    
    $var = preg_replace('/\s,/', ",", $var);
    
    $var = preg_replace('/,\s/', ",", $var);
    Thanks a lot!

    Jason

  2. #2
    Join Date
    Jul 2006
    Posts
    497
    Thanks
    8
    Thanked 70 Times in 70 Posts

    Default

    I don't understand your question... What's wrong with the code you posted?

    A couple notes:

    rtrim will only remove characters from the right, but trim does the same thing for both sides.
    Code:
    $var = trim(trim(str_replace(array("\t", "\r", "\n"), '', $var)), ',');
    Those last three lines can be combined:
    Code:
    $var = preg_replace('/\s{2,}/', " ", $var);
    $var = preg_replace('/(?:,{2,}|\s,|,\s)/', ",", $var);
    Last edited by Jesdisciple; 10-25-2008 at 08:28 PM. Reason: links, non-capturing group
    -- Chris
    informal JavaScript student of Douglas Crockford
    I like wikis - a lot.

  3. The Following User Says Thank You to Jesdisciple For This Useful Post:

    JasonDFR (10-26-2008)

  4. #3
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    Thanks Jedi,

    You answered my question exactly! So thanks!

    I knew that my code worked, but I also knew there was a way to combine all those lines together. Problem is, I didn't know how to combine the regex, so I wrote four instead of two.

    Could you explain your method of combining everything together in those last three lines?

    Thanks a lot!

    Jason

  5. #4
    Join Date
    Jul 2006
    Posts
    497
    Thanks
    8
    Thanked 70 Times in 70 Posts

    Default

    The parentheses set a group off from the rest of the regex, and the pipes (or bars) mean "OR" kind of like in PHP. So each instance of any of the sub-expressions between the parens and separated by the pipes will be replaced with a comma. See http://www.regular-expressions.info/alternation.html.

    The ?: after the first parenthesis makes the group "non-capturing," which slightly improves performance because PHP doesn't have to remember as much. See http://www.regular-expressions.info/brackets.html.

    P.S. You're the second person who's told me you thought my nick meant "Jedi" in some way, so I guess it's pretty confusing. It's actually supposed to be read "Jesus' disciple"... But I must admit Jedi has a cool ring to it.
    -- Chris
    informal JavaScript student of Douglas Crockford
    I like wikis - a lot.

  6. #5
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    I read too fast...

    Thanks again Jesdi!

    Jason

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
  •