for foreach textarea循环的php

发布于 2025-02-03 13:50:08 字数 537 浏览 4 评论 0原文

我需要从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/>";
  }
  }

}

I need get line by line data from textarea and group it for three cointainers. How to fix it?

My code

<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/>";
  }
  }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最初的梦 2025-02-10 13:50:09

您必须检查是否先发布null是否为null ...,因为帖子是无效的...
似乎Textarea将被编码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/>";
    }
  }
  }

}
}

you have to check if POST not null first... because POST is null able...
Seems textarea will be encode html...
so could you try to change

// '\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/>";
    }
  }
  }

}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文