【问题标题】:php loop through each string line in textareaphp循环遍历textarea中的每个字符串行
【发布时间】:2015-02-15 12:14:19
【问题描述】:

我正在使用此脚本从数组中的文本区域获取每一行:

//trim off excess whitespace off the whole
$text = trim(get_field('backlinks'));

//explode all separate lines into an array
$textAr = explode("\n", $text);

//trim all lines contained in the array.
$textAr = array_filter($textAr, 'trim');

//loop through the lines
var_dump($textAr);
foreach ($textAr as $line) {
    echo $line; 
}

var_dump 我的输出是:

 array(3) { [0]=> string(29) "
 http://www.nasa.gov/
 " [1]=> string(25) "http://www.cnn.com/
 " [2]=> string(27) "http://www.twitter.com/

 " }

echo $line; 的我的 html 输出是:

http://www.nasa.gov/<br>
http://www.cnn.com/<br>
http://www.twitter.com/

我的脚本处理错误。我需要echo $line; 的输出为:

http://www.nasa.gov/http://www.cnn.com/http://www.twitter.com/

我的var_dump 的输出是:

array(3) { [0]=> string(29) "http://www.nasa.gov/" [1]=> string(25) "http://www.cnn.com/" [2]=> string(27) "http://www.twitter.com/" }

我不希望我的输出/回显中出现换行符。我做错了什么?

提前致谢

【问题讨论】:

  • 你在 $text 变量中有什么?看来你那里有一些 br 标签
  • 您可能没有考虑到换行符可以由多个 \n 表示,但也可以有一个额外的 \r。 (或者在某些 [旧] 系统上一个 \r。)

标签: php foreach explode


【解决方案1】:
//trim off excess whitespace off the whole
$text = trim(get_field('backlinks'));
//explode all separate lines into an array
$textAr = explode("\n", $text);
//trim all lines contained in the array.
$textAr = array_filter($textAr, 'trim');
//loop through the lines
var_dump($textAr);
foreach($textAr as $line) {
    echo str_replace("\n","",str_replace("\r","",$line)); 
}

【讨论】:

    【解决方案2】:

    尝试使用 impload。

    回声内爆(",",$textAr) ;

    【讨论】:

      【解决方案3】:

      对不起,我以为是 PHP 代码,它是我的 textarea 的设置,它的广告是
      在每一行之后。

      【讨论】:

        猜你喜欢
        • 2014-06-29
        • 2018-10-19
        • 1970-01-01
        • 1970-01-01
        • 2016-01-24
        • 2010-11-30
        • 2010-12-02
        • 1970-01-01
        相关资源
        最近更新 更多