-1

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

4 回答 4

2

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

1) 简单地使用preg_replace()

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

2)你可以在这个上使用str_replace(),尽管代码看起来不那么干净:

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

3)你可以用trim()做到这一点

于 2013-10-29T10:55:52.423 回答
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.
于 2013-10-29T10:49:06.960 回答
0

好像有很多\r\n.

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

<?php
$string=str_replace("\r\n","",$string);
于 2013-10-29T10:50:30.480 回答
0

使用 str_replace 函数?

PHP 常见问题

于 2013-10-29T10:51:33.843 回答