从字符串中删除最后一个字符 [英] Remove last character from a string

查看:102
本文介绍了从字符串中删除最后一个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个国家列表作为数组..我希望此数组采用以下格式,以便以后通过ajax返回:-

I have a list of country as an array .. i want this array in following format to later return it via ajax :-

印度",美国",英国" ..

"india","usa","uk" ..

使用下面的代码来获得我正在寻找的..

Used following code to get somewhat i was looking for ..

foreach($country_list as $country) {
   $countries .= '"'.$country['label'].'",';
}

问题是它给出的输出是"india","usa","uk",...,即尾随逗号.

problem is it is giving output like "india","usa","uk", ... i.e with trailing comma .

试图用

substr_replace($countries, "0", -1);

rtrim($countries, ",");

但是没用! ..请帮忙!

But didnt work ! .. Please help !

推荐答案

我认为您不希望在修剪后将变量分配回去:

I think that you're missing to assign the variable back after the trim:

$s = '"india","usa","uk",';
$s = rtrim($s, ',');
// prints "india","usa","uk"
print $s;

演示

购买前先试

这篇关于从字符串中删除最后一个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆