为什么这个命令输出一个预期大小的空数组,而不是一个填充了元素的数组


Why does this command output an empty array of the expected size instead of one with the elements filled in?

我想让一个用户在文本区域中键入几行,然后让php将它们分配给一个数组,但是我的代码似乎将空值分配给数组

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>

带输入:

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

我得到输出:

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] => )

我传错阵了吗?我的正则表达式错了吗?

使用

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

其中''R表示行的末尾。

preg_split将分隔符作为第一个参数。但您尝试使用文本(而不是行尾)作为分隔符,并成功地只获得了行尾作为数组。

如果我很清楚你打算做什么,也许这个正则表达式是正确的:

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