I have a textarea in a form that allows users to enter a list of numbers separated by either a newline or a comma. I need the numbers they entered to be entered into an array. Unfortunately, the code I have does not work all the time. If users enter comma sepated data and a newline then the comma is left in the resulting array. In addition, if they add a newline at the end of the textfield then it enters an empty value into the array. The code I have is below:
$data = preg_split( "[\r\n]", $_POST[ 'textarea' ] );
if (count($data) == 1) {
$string = $data[0];
$data = explode(',', $string);
}
I was hoping for some help on how to get around the issues I am having.
“a list of numbers separated by either a newline or a comma”
So, you do not care which one it is, or if there is a comma and a newline? Then why not simply use all three characters equally as separators and allow “one or more” of them?
prints