可以使用以下几种方法去除字符串中的空格和换行符:
使用trim()函数去除字符串两端的空格和换行符:
$str = " Hello World! ";
$result = trim($str);
echo $result; // 输出:Hello World!
使用str_replace()函数将字符串中的空格和换行符替换为空字符串:
$str = "Hello\nWorld!";
$result = str_replace(array(" ", "\n"), "", $str);
echo $result; // 输出:HelloWorld!
使用preg_replace()函数结合正则表达式去除字符串中的空格和换行符:
$str = "Hello\nWorld!";
$result = preg_replace('/\s+/', '', $str);
echo $result; // 输出:HelloWorld!
以上方法可以根据具体需求选择适合的方法去除字符串中的空格和换行符。
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站