简体   繁体   中英

php loop through each string line in textarea

I am using this script to get every line from a textarea in an array:

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

My output from var_dump is:

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

 " }

My html output from the echo $line; is:

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

My script handles it wrong. I need the output of echo $line; to be:

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

And the output of my var_dump to be:

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

I don't want the line breaks in my output/echo. What am I doing wrong?

Thanks in advance

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

Try using impload.

echo implode(",",$textAr) ;

对不起,我以为这是PHP代码,这是我的textarea的设置,每行之后都会有一个<br>广告。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM