PHP如何移除最後一個字元?
在PHP中可以使用「rtrim()」函數去掉最後一個字符,該函數用於刪除字串末端的空白字符或其它字符,其語法是「rtrim(str,char) ”,其參數str表示要操作的字串,參數char表示要移除的字元。
使用範例
<?php $text = "\t\tThese are a few words :) ... "; $binary = "\x09Example string\x0A"; $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教程》
以上是PHP如何去除最後一個字元?的詳細內容。更多資訊請關注PHP中文網其他相關文章!