PHP如何去掉最後一個字元?

2020-07-16 10:06:30

PHP如何去掉最後一個字元?

在PHP中可以使用「rtrim()」函數去掉最後一個字元,該函數用於刪除字串末端的空白字元或者其它字元,其語法是「rtrim(str,char) 」,其引數str表示要操作的字串,引數char表示要去除的字元。

使用範例

<?php
$text = "ttThese are a few words :) ...  ";
$binary = "x09Example stringx0A";
$hello  = "Hello World";
var_dump($text, $binary, $hello);
print "n";
$trimmed = rtrim($text);
var_dump($trimmed);
$trimmed = rtrim($text, " t.");
var_dump($trimmed);
$trimmed = rtrim($hello, "Hdle");
var_dump($trimmed);
// 刪除 $binary 末端的 ASCII 碼控制字元
// (包括 0 - 31)
$clean = rtrim($binary, "x00..x1F");
var_dump($clean);
?>

以上例程會輸出:

string(32) "        These are a few words :) ...  "
string(16) "    Example string
"
string(11) "Hello World"
string(30) "        These are a few words :) ..."
string(26) "        These are a few words :)"
string(9) "Hello Wor"
string(15) "    Example string"
以上就是PHP如何去掉最後一個字元?的詳細內容,更多請關注TW511.COM其它相關文章!