【问题标题】:How to remove ASCII newline characters (nl) and carrier return characters (cr) from a string in php如何从php中的字符串中删除ASCII换行符(nl)和运营商返回字符(cr)
【发布时间】:2011-08-20 09:24:31
【问题描述】:

我必须从 php 字符串中删除和替换 ASCII 换行符 (nl) 和运营商返回字符 (cr)。

我尝试使用以下语句将 $input 中的所有 ASCII (nl) 字符替换为空格,但没有成功:

preg_replace('/[\x0a]+/',' ',$input);

然后我尝试用空格替换所有ASCII控制字符,以下是语句:

ereg_replace('[[:cntrl:]]', ' ', $encoded); // didn't work

我也尝试了以下语句,但没有运气:

ereg_replace("[:cntrl:]", "", $pString);
preg_replace('/[\x00-\x1F\x7F]/', '', $input);
preg_replace('/[\x00-\x09\x0B\x0C\x0E-\x1F\x7F]/', '', $input);

从 php 字符串中删除 ASCII 换行符 (nl) 和运营商返回字符 (cr) 的正则表达式是什么?

我提到了以下几个链接:
ASCII Table
Regular Expressions
Regular expression posix

【问题讨论】:

  • “以下链接可能对您有所帮助”,为什么他们不能帮助您? SO 不是“给我代码”类型的网站!

标签: php preg-replace ascii


【解决方案1】:

你不能只用str_replace吗?

str_replace( array("\n", "\r"), "", $stringinput );

【讨论】:

    【解决方案2】:

    为什么要使用正则表达式?怎么了

    str_replace(array("\n", "\r"), "", $string);
    

    ?在 PHP 中,字符 \n 和 \r 保证是实际的换行符和回车点:http://php.net/manual/en/language.types.string.php

    【讨论】:

      【解决方案3】:

      如果你坚持使用preg_replace() 来完成这样一个简单的任务,你可以使用:

      $result = preg_replace('/[\r\n]/', '', $subject);
      

      不过,您应该按照之前的建议使用str_replace(array("\n", "\r"), "", $string);

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-04-28
        • 2012-01-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-12-22
        相关资源
        最近更新 更多