如何从 php 字符串中删除 ASCII 换行符 (nl) 和载体返回字符 (cr)
我必须从 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 表
正则表达式
正则表达式 posix
I have to remove and replace ASCII newline characters (nl) and carrier return characters (cr) from a php string.
I tried using following statement to replace all ASCII (nl) char from $input with blank space but didn't work:
preg_replace('/[\x0a]+/',' ',$input);
then i tried to replace all the ASCII control characters with blank spaces, following is the statement:
ereg_replace('[[:cntrl:]]', ' ', $encoded); // didn't work
I tried the following statements also but no luck with them:
ereg_replace("[:cntrl:]", "", $pString);
preg_replace('/[\x00-\x1F\x7F]/', '', $input);
preg_replace('/[\x00-\x09\x0B\x0C\x0E-\x1F\x7F]/', '', $input);
What is the regex expression to remove ASCII newline characters (nl) and carrier return characters (cr) from a php string?
I referred to few link below :
ASCII Table
Regular Expressions
Regular expression posix
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你不能只使用 str_replace 吗?
Can't you just use str_replace?
为什么使用正则表达式?有什么问题吗
?在 PHP 中,字符 \n 和 \r 保证是实际的换行符和回车点: http://php.net/manual/en/language.types.string.php
Why use a regexp? What's wrong with
? In PHP, the characters \n and \r are guaranteed to be the actual newline and carriage return points: http://php.net/manual/en/language.types.string.php
如果您坚持使用
preg_replace()
来完成这样一个简单的任务,您可以使用:虽然,您应该使用
str_replace(array("\n", "\r "), "", $string);
如前所述。if you insist using
preg_replace()
for such a simple task you can use:Although, you should use
str_replace(array("\n", "\r"), "", $string);
as advised previoulsy.