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教程》
Atas ialah kandungan terperinci PHP如何去掉最后一个字符?. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!