2012-07-12 115 views
0

好吧,我確實有這個以下代碼從ob_get_clean()輸出刪除換行符

<?php 
ob_start(); 
?> 
codepad is an 
online compiler/interpreter, 
and a simple collaboration tool. 
Paste 

your code below, 
and codepad wi 
ll run 
it and give you a short 
URL you can use to share 
it in chat or email 
<?php 
$str = str_replace('\r\n','',trim(ob_get_clean())); 
echo $str; 
?> 

,你可以看到它是如何在這裏工作 http://codepad.org/DrOmyoY9

現在

什麼,我想在這裏是從去除新行ob_get_clean()的存儲輸出

我幾乎環顧了互聯網上關於如何刪除字符串中的換行符,這是除了使用緩慢的preg_replace()之外刪除換行符的常見和最快的方法。

爲什麼會發生這種情況?這已經是一個錯誤?或者我錯過了什麼?

+1

\ r \ n是windows風格。如果用戶使用linux \ n會怎麼樣?最佳陣列(「\ r」,「\ n」) – 2012-07-12 07:45:15

+0

謝謝你謝謝^^ – 2012-07-12 07:48:25

+0

@Dreadedsemicolon請發表您的答案我會接受它 – 2012-07-12 08:01:37

回答

1

\ r \ n

$str = str_replace("\r\n",'',trim(ob_get_clean())); 

是windows風格,但如果用戶使用Linux或Mac,它會有所不同。所以最好的解決方案是:

$str = str_replace(array("\r","\n"),'',trim(ob_get_clean())); 
2

我覺得你錯過了一個東西,它應該是:使用雙引號不是單引號

+0

哈哈謝謝^^ – 2012-07-12 07:48:00

+0

很高興幫忙。它解決了你的問題嗎? – 2012-07-12 07:58:49

+0

好吧,我加了「\ t」就可以了,但是我把它作爲我的問題的一位評論員 – 2012-07-12 08:02:30