Adam L.
Adam L.

Reputation: 187

Parse form textarea by comma or new line

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.

Upvotes: 6

Views: 11602

Answers (6)

Florian Mertens
Florian Mertens

Reputation: 2448

If you want to strip also multispaces to single spaces, trim spaces before and after, and allow also for semicolon ';' instead of just comma ',', then this code worked for me. I'm confident this could be done in a single preg_split and/or preg_replace though!

$string = '  1,2,3
   4,5,;,,
   ;6,
7
,   8   ,9; 10  ; 11;';

$tmp = preg_split("/[\r\n,;]+/", $string, -1, PREG_SPLIT_NO_EMPTY);
foreach($tmp as $val)
{
    $val = trim(preg_replace('!\s+!', ' ', $val));
    if($val != "")
        $data[] = $val;
}

var_dump($data);
/* Returns
array(11) {
  [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"
  [9]=>
  string(2) "10"
  [10]=>
  string(2) "11"
}
*/

Upvotes: 0

GZipp
GZipp

Reputation: 5416

$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"
}

Upvotes: 3

VolkerK
VolkerK

Reputation: 96159

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

Upvotes: 12

Mewp
Mewp

Reputation: 4715

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

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

Upvotes: 0

Poppanator
Poppanator

Reputation: 89

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

Upvotes: 0

Sarfraz
Sarfraz

Reputation: 382726

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' ]);

Upvotes: 0

Related Questions