Log in

View Full Version : Regular Expression Help CSV



JasonDFR
10-24-2008, 10:57 AM
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?


$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

Jesdisciple
10-25-2008, 08:20 PM
I don't understand your question... What's wrong with the code you posted?

A couple notes:

rtrim (http://www.php.net/manual/en/function.rtrim.php) will only remove characters from the right, but trim (http://www.php.net/manual/en/function.trim.php) does the same thing for both sides.
$var = trim(trim(str_replace(array("\t", "\r", "\n"), '', $var)), ',');

Those last three lines can be combined:
$var = preg_replace('/\s{2,}/', " ", $var);
$var = preg_replace('/(?:,{2,}|\s,|,\s)/', ",", $var);

JasonDFR
10-26-2008, 06:52 AM
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

Jesdisciple
10-26-2008, 06:25 PM
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.

JasonDFR
10-26-2008, 06:49 PM
I read too fast...

Thanks again Jesdi!

Jason