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
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