php去掉字符串里空格的方法有哪些

php去掉字符串中的空格的方法有两种:1、定义变量$str,用于保存要更改的字符串,使用"str_replace()"函数,用一个空字符串替换所有空格,语法为"str_replace(' ', '', $str);2、"preg_replace()"函数,使用正则匹配,语法为"preg_replace('/s+/', '', $str)"。

php去掉字符串里空格的方法有哪些(图1)

本教程操作系统:Windows10系统、php8.1.3版本、Dell G3电脑。

php去掉字符串中的空格的方法有两种:

1、使用 str_replace()

$str = "This is a string with spaces.";
$new_str = str_replace(' ', '', $str);
echo $new_str; // Thisisastringwithspaces.

发表评论

加入本站VIP会员订阅计划,海量资源免费查看

目前为止共有 3654 位优秀的VIP会员加入! 立刻加入VIP会员