2013-10-29 47 views
-1

我必须通过PHP从给定内容中删除换行符和小车。 请告诉我如何?在PHP中格式化内容

<p>\r\n \r\n  \r\n   \r\n  \r\n  \r\n   \r\n   \r\n  \r\n  \r\n   \r\n   \r\n  \r\n  \r\n   \r\n   \r\n  \r\n  \r\n   \r\n   \r\n  \r\n  \r\n   \r\n   \r\n  \r\n  \r\n   \r\n   \r\n  \r\n \r\n</p> 
<table width="\&quot;100%\&quot;" class="\&quot;table-view\&quot;" cellpadding="\&quot;0\&quot;" cellspacing="\&quot;0\&quot;"> 
    <tbody> 
     <tr> 
      <th colspan="\&quot;2\&quot;" align="\&quot;left\&quot;">Specifications</th> 
     </tr> 
     <tr> 
      <td valign="\&quot;top\&quot;"><strong>Frequency bands</strong></td> 
      <td>\r\n 
      <ul>\r\n 
       <li>380 - 400 MHz</li> 
       \r\n 
       <li>410 - 430 MHz</li> 
       \r\n 
       <li>806 - 825, 851 - 870 MHz*</li> 
       \r\n   </ul> 
       \r\n</td> 
      </tr> 
      <tr> 
       <td valign="\&quot;top\&quot;"><strong>Power class</strong></td> 
       <td>\r\n 
       <ul>\r\n 
        <li>EN 300392-2 compliant, power class 4</li> 
        \r\n 
        <li>Receiver class A</li> 
        \r\n 
        <li>RF power control, 4 steps of 5dB</li> 
        \r\n   </ul> 
        \r\n</td> 
       </tr> 
       <tr> 
        <td valign="\&quot;top\&quot;"><strong>Size</strong></td> 
        <td>\r\n 
        <ul>\r\n 
         <li>Weight: 292 g</li> 
         \r\n 
         <li>Dimensions: 157 x 57 x 35 mm</li> 
         \r\n   </ul> 
         \r\n</td> 
        </tr> 
        <tr> 
         <td valign="\&quot;top\&quot;"><strong>Durability</strong></td> 
         <td>\r\n 
         <ul>\r\n 
          <li>High-resolution, active TFT colour display</li> 
          \r\n 
          <li>Up to 65,536 colours with 130x130 pixels</li> 
          \r\n 
          <li>Display texts in more than 20 languages</li> 
          \r\n 
          <li>Support for Latin, Arabic, Greek, Chinese and Korean</li> 
          \r\n   </ul> 
          \r\n</td> 
         </tr> 
         <tr> 
          <td valign="\&quot;top\&quot;"><strong>Keypad/Controls</strong></td> 
          <td>\r\n 
          <ul>\r\n 
           <li>2-sided user interface</li> 
           \r\n 
           <li>Alphanumeric keypad</li> 
           \r\n 
           <li>4 navigation keys, 3 selection keys</li> 
           \r\n 
           <li>HI/LO key for loudspeaker control</li> 
           \r\n 
           <li>Power-on key, volume keys, red function key, duty key, fast menu key, group selector, back key</li> 
           \r\n   </ul> 
           \r\n</td> 
          </tr> 
          <tr> 
           <td valign="\&quot;top\&quot;"><strong>GPS receiver</strong></td> 
           <td>\r\n 
           <ul>\r\n 
            <li>Sensitivity &ndash;152 dBm</li> 
            \r\n 
            <li>Cold start accuracy (open sky)*<br /> 
            \r\n    5 metres (50% confidence level)<br /> 
            \r\n    10 meters (95% confidence level)<br /> 
            \r\n    * measured at &ndash;130 dBm<br /> 
            \r\n    HI/LO key for loudspeaker control</li> 
            \r\n 
            <li>GPS activity indicator</li> 
            \r\n   </ul> 
            \r\n</td> 
           </tr> 
          </tbody> 
         </table> 
+0

那么,你卡在哪里?你有没有读过你的文件/数据?您是否编写了一些代码来删除内容?你在哪里?我猜这不起作用?所以告诉我们。 – Bart

+0

类似线程, http://stackoverflow.com/questions/3059091/how-to-remove-carriage-returns-from-output-of-string –

+0

$海峡= str_replace函数( “\ r \ n”, “”,$海峡); – user2092317

回答

2

你可以以三种方式做到这一点

1)只需使用preg_replace()

$str = preg_replace('~[\r\n]+~', '', $str); 

2)你可以逃脱str_replace()就这一个,虽然代码不看起来很干净:

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

3)你可以这样做trim()

1

使用PHP trim()功能来清除前/后的字符集:

" " (ASCII 32 (0x20)), an ordinary space. 
"\t" (ASCII 9 (0x09)), a tab. 
"\n" (ASCII 10 (0x0A)), a new line (line feed). 
"\r" (ASCII 13 (0x0D)), a carriage return. 
"\0" (ASCII 0 (0x00)), the NUL-byte. 
"\x0B" (ASCII 11 (0x0B)), a vertical tab. 
+0

内核\ r和\ n怎么样? – user2745375

+0

使用[str_replace](http://php.net/str_replace)功能。我的建议是基于你正在逐行阅读的前提。 – Latheesan

0

好像有很多\r\n.

因此,一个简单str_replace就足够了。

<?php 
$string=str_replace("\r\n","",$string); 
+0

字符串不是普通的字符串。它里面嵌入了一些HTML。请告诉我如何通过PHP从HTML代码中删除换行和传送 – user2745375