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如何去掉最后一个字符?的详细内容。