0

I ultimately want to get the text pasted into a textarea to format into an html table. The data input into the textarea would be multiple rows and columns of data.

I have been able to get the individual rows without too much trouble but I'd need to be able to get rows AND columns in order to get them into a table properly.

One issue I have is the columns are not necessarily divided by the same character. The input tends to have multiple spaces between each column (those could be trimmed down to 1 somehow perhaps).

Here is an example of the code I have that spits out the individual lines.

if (isset($_POST['Submit1'])) {

$test = isset($_POST['test'])?$_POST['test']:"";
$testlines = explode("\n", str_replace("\r", "", $test));
foreach($testlines as $lines) {

echo $lines . "</br>";

}
}

Based on a suggestion I tried using the preg_split and it now outputs a table.

if (isset($_POST['Submit1'])) {

$test = isset($_POST['test'])?$_POST['test']:"";
$testlines = explode("\n", str_replace("\r", "", $test));

echo "<table>";
foreach($testlines as $lines) {
echo "<tr>";
$cols = preg_split('/\s+|,/',$lines);

foreach($cols as $field){
echo "<td>" . $field . "</td>";
}
echo "</tr>";
}
echo "</table>";


}
4

1 回答 1

0

如果您使用preg_split分隔行,您可以使用多个分隔符和正则表达式可以表达的任何内容,甚至多个空格一起使用。这是一个用一个或多个空格和逗号分隔的示例

$cols = preg_split('/\s+|,/',$lines)

用于|分隔不同的拆分选项。\s是空格字符(或等效字符),并且+表示该字符的一次或多次。

于 2013-08-29T23:35:21.893 回答