php for loop with foreach textarea



我需要从textarea获得逐行数据,并将其分组为三个容器。如何解决这个问题?

我的代码
<form method="POST" action="test.php">
<textarea name="textarea" style="width:100%; height: 20vh"></textarea><br /><br />
<input type="submit" class="button" style="float: right; cursor:pointer;" value="">
</form>
<?php
$lines = explode("n", $_POST['textarea']);
if ( !empty($lines) ) {
foreach ( $lines as $line ) {
for ($i=1;$i<=3;$i++) {
echo trim( $line )."<br/>";
}
}
}

你必须先检查POST是否为空…因为POST是空的…似乎文本区将编码html…所以你能不能试着改变

// 'n' to '<br>' or '</br>' !

if(trim($_POST['textarea'])!=null){
$lines = explode("n", $_POST['textarea']); //or change 'n' to <br>  or </br> if this not working
if ( $lines != null) ) {
$i=0;
foreach ( $lines as $line ) {
if( !empty(trim($line)) ){
$i++;
if($i<=3) {
echo trim( $line )."<br/>";
}
}
}
}
}

最新更新