我有一个表单,它需要用户输入;最近,我遇到了许多用户输入和多个空格。例如, “我 测试 正在 很好!“ 有没有什么办法可以摆脱PHP级别或MySQL级别的这些空白?摆脱在php或mysql中的多个空格
- 显然修剪不起作用。
- 我正在考虑使用递归函数,但不知道是否有一个简单而快速的方法来做到这一点。
到目前为止我的代码是如下:
function noWhiteSpaces($string) {
if (!empty($string)) {
$string = trim($string);
$new_str = str_replace(' ', ' ', $string);
} else {
return false;
}
return $new_str;
}
echo noWhiteSpaces("My tests are working fine here !");
输入的实际空白('\ t','\ n',空格等)还是像' '这样的东西?有一个区别。 – Keelan