在PHP中格式化内容

时间:2013-10-29 10:47:08

标签: php function string-formatting

我必须通过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>

4 个答案:

答案 0 :(得分:2)

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

1)只需使用preg_replace()

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

2)虽然代码看起来不干净,但你可以在这个问题上取消str_replace()

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

3)您可以使用trim()

执行此操作

答案 1 :(得分: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.

答案 2 :(得分:0)

似乎有很多 \r\n.

所以一个简单的 str_replace 就足够了。

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

答案 3 :(得分:0)

使用str_replace函数吗?

PHP faq