doulan3966 2013-10-17 05:18
浏览 36
已采纳

使用php [duplicate]避免textarea中的空白行

This question already has an answer here:

I am using below php code to process two textareas called namelist and placelist and echo it to html.

<?php

$namelist = $_POST['namelist'];
$placelist = $_POST['placelist'];

$names = explode("
", $namelist); 
$places = explode("
", $placelist); 

$entries = min(count($names), count($places));

for ($i = 0; $i < $entries; $i++) {
$name  = trim($names[$i]); 
$place = trim($places[$i]);
echo "My name is $name and I am from $place ".PHP_EOL;
}

?>

But above code processing the blank lines in my textareas ? I need to avoid blank lines from echoing. for example, if the namelist contains

Tom
George

and placelist contains

GK
US

I will get output like:

My name is Tom and I am from GK 
My name is George and I am from US

But If there is a blank line in an of the textarea it processing the blank line too. eg:

Tom
George

and

GK

US

It will give below output like

My name is Tom and I am from GK 
My name is George and I am from
</div>
  • 写回答

2条回答 默认 最新

  • douchen7555 2013-10-17 06:54
    关注

    Try this:

    Using regex to eliminate blank lines before exploding (works well for any number of consecutive blank lines)

    $name = preg_replace('/
    +/', "
    ", trim($_POST['namelist']));
    $place = preg_replace('/
    +/', "
    ", trim($_POST['placelist']));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置