簡體   English   中英

php遍歷textarea中的每個字符串行

[英]php loop through each string line in 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/

 " }

我的html輸出來自echo $line; 是:

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

我的腳本處理錯誤。 我需要echo $line;的輸出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/" }

我不想在輸出/回聲中換行。 我究竟做錯了什么?

提前致謝

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

嘗試使用impload。

echo implode(“,”,$ textAr);

對不起,我以為這是PHP代碼,這是我的textarea的設置,每行之后都會有一個<br>廣告。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM