2012-10-09 28 views
-1

可能重複:
How to do unfolding RFC 822
Parsing e-mail-like headers (similar to RFC822)正則表達式 「固定」 的電子郵件標題,使他們一行

我有一個類似於E-一些輸入數據電子郵件數據,長行被包裝到下一行。例如:

robot-useragent: ABCdatos BotLink/1.0.2 (test links) 
robot-language: basic 
robot-description: This robot is used to verify availability of the ABCdatos 
        directory entries (http://www.abcdatos.com), checking 
        HTTP HEAD. Robot runs twice a week. Under HTTP 5xx 
        error responses or unable to connect, it repeats 
        verification some hours later, verifiying if that was a 
        temporary situation. 

robot-description字段是「太長」一行,並且纏繞到下一個。在分析這些數據的幫助,我想拿出可與preg_replace()可以使用正則表達式符合下列條件取代:

  • 新行字符跟空格
  • 更換新線的字符,隨後附加的新行字符

輸出示例:

robot-description: This robot is used to verify availability of the ABCdatos directory entries (http://www.abcdatos.com), checking HTTP HEAD. Robot runs twice a week. Under HTTP 5xx error responses or unable to connect, it repeats verification some hours later, verifiying if that was a temporary situation. 

我是RegEx的新手。我怎樣才能建立這樣的表達?如果您選擇回答,請在表達式中包含組件的簡要說明。我真的很想學習如何做到這些。

我已經開始了:\n([^\S])*它很接近。 http://codepad.org/iMObpgFX

+0

@MarcB,這不重複。在我的另一個問題中,我正在詢問如何以類似於內置IMAP函數的方式處理標題。在這個問題中,我特別要求關於RegEx重新加入系列。在我看來,這些完全是分開的問題。雖然他們與我有相同的目標,但我想知道兩者的解決方案。如果您不同意,請告訴我。 – Brad

回答

0

事實證明,這個問題是一個重複的問題,但與Marc提到的問題不同。

答案:

$output = preg_replace('/\r\n(?:[ \t]+)/', '', $input); 

從這裏:https://stackoverflow.com/a/4227885/362536

我已經投票決定關閉這個問題,因爲我不能刪除它,因爲它有答案。我會標誌着國防部的關注。

1

也許你可以試試:

(\r|\n)\s+

(\r|\n) # matches both newline and carriage return 
\s+  # any whitespace (tabs, spaces, new lines) 

Try it

+0

謝謝你的回答。不幸的是,這似乎把一切都放在一條線上。 http://codepad.org/zGRAzqhM – Brad

+0

鮑里斯,我看到它在你的例子中工作。任何想法爲什麼鍵盤版本不是?是否有一些額外的選項需要指定? – Brad

+0

它也在鍵盤上工作,查看源代碼或使用'nl2br()'(新行不用HTML解釋) –