2011
Sep
25

先介紹一下,而謂換行符號:

  • 一般換行字符為 \n chr(10) , enter鍵為 \r chr(13),又稱回車符,return的意思。
  • 在windows系统中,換行符號為:\r\n (先enter在換行) ,若是用ultraEdit來檢視16進位編碼,會看到 0D 0A 。
  • 而在unix系統中,換行符號只會有 \n ,所以unix編輯出來的檔案在ultraEdit中只會看到 0A 換行。

textarea 中 的換行字符

  • Firefox 中 textarea 換行符號只有\n。
  • IE8 中 textarea 換行符號為 \r\n
  • 假設一個 textarea 的內容如下
Example
  1. <textarea id="textarea">
  2. Line1
  3. Line2
  4. Line3</textarea>
  • 在Firefox中,實際的值為
Example
  1. <textarea id="textarea">
  2. Line1\n
  3. Line2\n
  4. Line3\n</textarea>
  • 在IE8中,實際的值為
Example
  1. <textarea id="textarea">
  2. Line1\r\n
  3. Line2\r\n
  4. Line3\r\n</textarea>

在一般 html tag 中的換行字符

如果我們打開 html source code 來看,換行的字符如下。

  • Firefox 一樣只有\n換行。
  • IE8 中換行字符會變成空白。
  • 假設一個div的內容如下
Example
  1. <div id="div">
  2. Line1
  3. Line2
  4. Line3</div>
  • 在Firefox 實際內容為
Example
  1. <div id="div">
  2. Line1\n
  3. Line2\n
  4. Line3\n</div>
  • 在IE8 實際內容為
Example
  1. <div id="div">
  2. Line1 Line2 Line3</div>

換行字符轉換

在 HTML 中如果要看到正確的換行效果,應該要使用的 html tag 為 <br />,所以我得把 \n\r 轉成 <br />。

PHP Example
  1. $REG = '/[\n\r]+/';
  2. $html = preg_replace($REG, '<br />', $html);
  3.  

回應 (Leave a comment)