如何从字符串中删除/删除最后一个字符php
问题描述:
如何从php字符串变量中动态删除最后一个字符。如何从字符串中删除/删除最后一个字符php
$string = '10,20,30,';
echo $string;
可用输出:
10,20,30,
所需的输出:
10,20,30
答
rtrim($string, ",");
在字符串的端移除逗号。
trim($string, ",");
删除逗号在开头和结尾的字符串的。
您还可以使用内爆,如果你使用数组:
来自实例php.net:
<?php
$array = array('lastname', 'email', 'phone');
$comma_separated = implode(",", $array);
echo $comma_separated; // lastname,email,phone
// Empty string when using an empty array:
var_dump(implode('hello', array())); // string(0) ""
?>
答
你可以使用
rtrim($string, ',');
但我认为你的问题在别处。好像你正在for/while/foreach循环中为这个字符串添加选项。
使用这个代替:
$string = array[];
foreach ($parts as $part) {
$string[] = $part;
}
$string = implode($string, ',');
答
echo substr($string, 0, strlen($string)-1);
回声RTRIM($字符串, ''); –
[删除字符串中的最后一个字符]可能的重复(http://stackoverflow.com/questions/5592994/remove-the-last-character-from-string) –
'echo substr($ string,0,strlen($字符串)-1);' – connexo