0

I want to have a user type in several lines into a textarea and then have php assign them to an array, however the code I have seems to assign blank values to the array

HTML:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtm$
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<h1>Juniper to IOS Translator</h1>

<form method="post" action="query.php">
 <textarea name="txtarea" style="width: 80%; height: 25em;">
 </textarea>
 <input type="submit" />
</form>

PHP:

<?php

$text = $_POST["txtarea"];

echo nl2br($text);
echo "<br />" , "<br />";

$data = preg_split("/((?<=\n).*?(?=\n))/", $text , -1, PREG_SPLIT_NO_EMPTY);

print_r(array_values($data));
?>
</body>
</html>

With the input:

Line with stuff 
Line with stuff more
Line with stuff more more
Line with stuff more more more
Line with stuff more more more more

I get the output:

Line with stuff 
Line with stuff more
Line with stuff more more
Line with stuff more more more
Line with stuff more more more more


Array ( [0] => [1] => [2] => [3] => [4] => [5] => )

am I passing the array wrong? is my regex wrong?

Mark Omo
  • 37
  • 4

2 Answers2

0

Use

$data = preg_split("/\R/", $text , -1, PREG_SPLIT_NO_EMPTY);

Where \R symbolize end of line.

preg_split takes a delimiter as first param. But you tried to use text(not end of line)as delimiter and successfully got only end of lines as array.

sectus
  • 15,605
  • 5
  • 55
  • 97
0

if I understand well what You're planning to do, maybe this regex is the right one:

$data = preg_split("/\n/", $text , -1, PREG_SPLIT_NO_EMPTY);