php循环遍历textarea中的每个字符串行

时间:2015-02-15 12:14:20

标签: php foreach explode

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

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

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

提前致谢

3 个答案:

答案 0 :(得分: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)); 
}

答案 1 :(得分:-1)

尝试使用impload。

echo implode(“,”,$ textAr);

答案 2 :(得分:-1)

对不起,我以为这是PHP代码,它是我的textarea的设置,其中的广告是      &LT; br&gt;  在每一行之后。