在这个程序中,我们使用内置函数 str_replace() 从字符串中删除空格。例如,如果文本是 "Welcome to PHP learning platform" 那么输出将是 "WelcometoPHPlearningplatform"。
要从字符串中删除空格,我们使用 str_replace() 并将空格替换为空,通过这样做,空格将从字符串中删除。
str_replace() 的语法
str_replace(find,replace,string,count)
步骤 1: 将字符串分配给变量 string
步骤 2: 打印变量 string 的值,作为删除空格之前的字符串
步骤 3: 使用内置函数 str_replace() 删除空格
步骤 4: 打印变量 string 的值,作为删除空格之后的字符串
<?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