PHP 程序,用于从字符串中删除空格


2022年5月1日, Learn eTutorial
2142

从字符串中删除空格是什么意思?

在这个程序中,我们使用内置函数 str_replace() 从字符串中删除空格。例如,如果文本是 "Welcome to PHP learning platform"  那么输出将是 "WelcometoPHPlearningplatform"。

如何使用 PHP 从字符串中删除空格?

要从字符串中删除空格,我们使用 str_replace() 并将空格替换为空,通过这样做,空格将从字符串中删除。

str_replace() 的语法


str_replace(find,replace,string,count)

 

算法

步骤 1: 将字符串分配给变量 string

步骤 2: 打印变量 string 的值,作为删除空格之前的字符串

步骤 3: 使用内置函数 str_replace() 删除空格

步骤 4: 打印变量 string 的值,作为删除空格之后的字符串

PHP 源代码

                                          <?php
$string = "learne tutorials . com";
echo "String before removing the white spaces: $string \n";
$string = str_replace(' ', '', $string);
echo "String after removing the white spaces: $string";
?>
                                      

输出

String before removing the white spaces: learne tutorials . com
String after removing the white spaces: learnetutorials.com