开发者

Parse form textarea by comma or new line

开发者 https://www.devze.com 2023-01-12 01:45 出处:网络
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 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?

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


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


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


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


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

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


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"
}
*/
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号