【问题标题】:Formatting Content in PHP [closed]在 PHP 中格式化内容 [关闭]
【发布时间】:2013-11-08 11:30:17
【问题描述】:

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

【问题讨论】:

  • 好吧,你卡在哪里了?你读过你的文件/数据吗?您是否编写了一些代码来删除内容?你到哪里去了?估计没用吧?所以告诉我们。
  • $str=str_replace("\r\n","",$str);
  • str_replace(array("\r", "\n"), "") 怎么样?
  • 实际上,上述数据存储在我的数据库中。我必须通过删除回车符和换行符来格式化它,然后再将其显示在前端。str_replace 不起作用

标签: php function string-formatting


【解决方案1】:

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

1) 只需使用preg_replace()

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

2) 尽管代码看起来不那么干净,但您可以使用 str_replace() 逃脱:

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

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

【讨论】:

    【解决方案2】:

    使用 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.
    

    【讨论】:

    • 内层 \r 和 \n 呢?
    • 使用str_replace函数。我的建议是在您逐行浏览这些内容的前提下。
    【解决方案3】:

    好像有很多\r\n.

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

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

    【讨论】:

    • string 不是普通的字符串。里面嵌入了一些 HTML。你能告诉我如何通过 PHP 从 HTML 代码中删除换行符和回车
    【解决方案4】:

    使用 str_replace 函数?

    PHP faq

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-01
      • 2017-07-22
      • 1970-01-01
      相关资源
      最近更新 更多