2011-11-18 45 views
2
<textarea style="resize: none;"> 
<?php 
while ($row = mysql_fetch_array($result)) { 
    echo $row[1] . "\n"; 
} 
    ?> 
</textarea> 

我想阻止最後一個值有一個換行符 - 我該怎麼做呢?我不想要底部的白色空間。如何防止上一次換行?

回答

5
<textarea style="resize: none;"> 
<?php 
    $rows = array(); 
    while ($row = mysql_fetch_array($result)) { 
     $rows[] = $row[1]; 
    } 
    echo implode($rows, "\n"); 
?> 
</textarea> 
+0

,似乎工作。像Python中的.join一樣運行。 –

+2

僅供參考,'implode()'將參數置於任一位置,但爲了與'explode()'一致,首選訂單是$ glue,$ pieces' – hafichuk

+0

+1。 :) – Herbert

0

我平時做這樣的事情:

<textarea style="resize: none;"> 
    <?php 
     while ($row = mysql_fetch_array($result)) { 
      if($stuff==''){ 
       $stuff=$row[1]; 
      }else{ 
       $stuff.= "\n" . $row[1]; 
      } 
     } 
     print $stuff; 
    ?> 
</textarea> 
0
<?php 
$results = array(); 

while ($row = mysql_fetch_array($result)) 
    $results[] = $row[1]; 

echo implode("\n", $results); 
?> 

<?php 
$first = true; 
while ($row = mysql_fetch_array($result)) { 
    if (!$first) 
     echo "\n"; 
    echo $row[1]; 
    $first = false; 
} 
?> 
0

保存在一個變量,然後從該字符串的最後一個字符的行。

<?php 
$content = ''; 
while ($row = mysql_fetch_array($result)) { 
    $content .= $row[1] . "\n"; 
} 
echo substr($content, 0, strlen($content) - 1); 
?> 
0

這應該工作:

<?php 
$string = ''; 
while ($row = mysql_fetch_array($result)) { 
    $string .= $row[1] . "\n"; 
} 
?> 
<textarea style="resize: none;"> 
    <?php echo trim($string,"\n"); ?> 
</textarea> 
0

一個辦法是做這樣的:

<textarea style="resize: none;"> 
<?php 
    $prefix= ''; 
    while ($row = mysql_fetch_array($result)) { 
     echo $prefix . $row[1]; 
     if($prefix == ''){ 
     $prefix = "\n"; 
     } 
?> 
</textarea>