當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


PHP string trim()用法及代碼示例

PHP 字符串 trim() 函數是內置函數。它用於從字符串的開頭和結尾或字符串的兩側去除空格。

用法:

trim(string,charlist);
參數 描述 必需/可選
String 指定要檢查的字符串。 Required
charlist 指定要刪除的字符。以下字符:
  • "\0" - NULL
  • "\t" - 選項卡
  • "\n" - 新線
  • "\x0B" - 垂直製表符
  • "\r" - 回車
  • " " - 普通空白
Optional

例子1

<?php
$str = "Hello PHP!";
echo "Your string is:".$str ."<br>";
echo "By using trim() function:".trim($str,"Hed!");
?>

輸出:

Your string is:Hello PHP!
By using trim() function:llo PHP

例子2

<?php
$str = "  Hello Javatpoint!  ";
echo "Without trim() function:".$str;
echo "<br>";
echo "With trim() function:".trim($str);
?>

輸出:

Without trim() function:Hello Javatpoint! 
With trim() function:Hello Javatpoint!

例子3

<?php
$str = "\n\n\nHello Javatpoint\n\n\n";
echo "Without trim() function:" . $str;
echo "<br>";
echo "With trim() function:" . trim($str);
?>

輸出:

Without trim() function:Hello Javatpoint 
With trim() function:Hello Javatpoint






相關用法


注:本文由純淨天空篩選整理自 PHP string trim() function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。