我得到$row['message']从一个MySQL数据库,我需要删除所有的空白,如\n \t等。
$row['message'] = "This is a Text \n and so on \t Text text.";
应格式化为:
$row['message'] = 'This is a Text and so on Text text.';
我试着:
$ro = preg_replace('/\s\s+/', ' ',$row['message']);
echo $ro;
但它不删除\n或\t,只删除单个空格。有人能告诉我怎么做吗?
<?php
$str = "This is a string with
spaces, tabs and newlines present";
$stripped = preg_replace(array('/\s{2,}/', '/[\t\n]/'), ' ', $str);
echo $str;
echo "\n---\n";
echo "$stripped";
?>
这个输出
This is a string with
spaces, tabs and newlines present
---
This is a string with spaces, tabs and newlines present
<?php
$str = "This is a string with
spaces, tabs and newlines present";
$stripped = preg_replace(array('/\s{2,}/', '/[\t\n]/'), ' ', $str);
echo $str;
echo "\n---\n";
echo "$stripped";
?>
这个输出
This is a string with
spaces, tabs and newlines present
---
This is a string with spaces, tabs and newlines present