PHP如何去掉最后一個字符?
在PHP中可以使用“rtrim()”函數(shù)去掉最后一個字符,該函數(shù)用于刪除字符串末端的空白字符或者其它字符,其語法是“rtrim(str,char) ”,其參數(shù)str表示要操作的字符串,參數(shù)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教程》