tags:

views:

135

answers:

5

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: 

One of the possible reasons could be that you are missing delimiter and + to find one ore more, instead of this:

$data = preg_split( "[\r\n]", $_POST[ 'textarea' ] );

Try:

$data = preg_split("/[\r\n]+/", $_POST[ 'textarea' ]);
Sarfraz
A: 

You should "trim" the user input from white space:

$data = preg_split("[\r\n"], trim($_POST["textarea"]));

This will remove leading and trailing white space. You can also remove leading and trailing commas - to prevent empty array indices when csv is used - by specifying a comma as second argument to trim:

trim($data, ",");

For more info on trim see http://php.net/trim

Poppanator
A: 
$data = preg_split( "/[\r\n]+|,/", trim($_POST['textarea']) );

This will split by either newlines or commas, and eliminate any trailing newlines.

Mewp
I have tried your code but, unfortunately, it is not splitting the form data correctly if there are both commas and newlines. For example, if a user inputs:3, (newline here)4, (newline here)4, (newline here)6Then it cannot handle it. I would like to sort this out server side if possible.
Adam Lund
What I mean to say is that it separates twice.
Adam Lund
A: 

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

<?php
$input = '1,2,3
4,5,,,,
,6,
7
,8,9';

$data = preg_split("/[\r\n,]+/", $input, -1, PREG_SPLIT_NO_EMPTY);
var_dump($data);

prints

array(9) {
  [0]=>
  string(1) "1"
  [1]=>
  string(1) "2"
  [2]=>
  string(1) "3"
  [3]=>
  string(1) "4"
  [4]=>
  string(1) "5"
  [5]=>
  string(1) "6"
  [6]=>
  string(1) "7"
  [7]=>
  string(1) "8"
  [8]=>
  string(1) "9"
}
VolkerK
Perfect - thank you! I will look up the extra parts of preg_split you have put in so that I fully understand what you have done.
Adam Lund
A: 
$input = '1,2,3
4,5,,,,
,6,
7
,8,9
';

$data = str_replace(array("\r", "\n"), ',', $input);
$data = array_filter(explode(',', $data));

var_dump(array_values($data));

prints

array(9) {
  [0]=>
  string(1) "1"
  [1]=>
  string(1) "2"
  [2]=>
  string(1) "3"
  [3]=>
  string(1) "4"
  [4]=>
  string(1) "5"
  [5]=>
  string(1) "6"
  [6]=>
  string(1) "7"
  [7]=>
  string(1) "8"
  [8]=>
  string(1) "9"
}
GZipp