PHP ltrim() 函式
定義和用法
ltrim() 函式用於從字串的左側刪除空格或其他預定義字元。
相關函式
語法
ltrim(string,charlist)
引數值
引數 | 描述 |
---|---|
string | 必需。指定要檢查的字串 |
charlist | 可選。指定要從字串中移除的字元。如果省略,則移除所有以下字元
|
技術詳情
返回值 | 返回修改後的字串 |
---|---|
PHP 版本 | 4+ |
更新日誌 | charlist 引數在 PHP 4.1 中新增 |
更多示例
示例
從字串的左側刪除空格
<?php
$str = " Hello World!";
echo "Without ltrim: " . $str;
echo "<br>";
echo "With ltrim: " . ltrim($str);
?>
上述程式碼的 HTML 輸出是 (檢視原始碼)
<!DOCTYPE html>
<html>
<body>
Without ltrim: Hello World!<br>With ltrim: Hello World!
</body>
</html>
上述程式碼的瀏覽器輸出是
Without ltrim: Hello World!
With ltrim: Hello World!
自己動手試一試 »
示例
從字串的左側刪除換行符 (\n)
<?php
$str = "\n\n\nHello World!";
echo "Without ltrim: " . $str;
echo "<br>";
echo "With ltrim: " . ltrim($str);
?>
上述程式碼的 HTML 輸出是 (檢視原始碼)
<!DOCTYPE html>
<html>
<body>
Without ltrim
Hello World!<br>With ltrim: Hello World!
</body>
</html>
上述程式碼的瀏覽器輸出是
Without ltrim: Hello World!
With ltrim: Hello World!
自己動手試一試 »
❮ PHP 字串參考