PHP 中如何从开头删除多余的空格?

phpserver side programmingprogramming更新于 2025/8/18 4:22:17

如果您想从开头删除多余的空格,请在 PHP 中使用 ltrim()。假设我们的变量是 −

$value="  Hello, welcome to PHP Tutorial  ";

我们希望输出左侧没有空格 −

Hello, welcome to PHP Tutorial

示例

<!DOCTYPE html>
<html>
<body>
<?php
   $value="  Hello, welcome to PHP Tutorial  ";
   print_r( "The actual value is=".$value."<br>");
   echo "The actual length is=",strlen($value)."<br>";
   $modifiedValue=ltrim($value);
   echo "After removing extra whitespace from beginning=",strlen($modifiedValue)."<br>";
   echo "The result is=".$modifiedValue;
?>
</body>
</html>

输出

这将产生以下输出 −

The actual value is= Hello, welcome to PHP Tutorial
The actual length is=34
After removing extra whitespace from beginning=32
The result is=Hello, welcome to PHP Tutorial

相关文章