我想摆脱最后一个逗号php

时间:2015-08-17 05:24:46

标签: php arrays comma

我想摆脱这句话中的最后一个逗号,它在一个从数组中拉出的变量中。

$Title = aa, bb, cc, dd, ee, ff, gg, hh, ii, jj,

我知道你通常使用implode() or rtrim($Title, ","),但那些通常用于数组,不是吗?

我想要一个不是用于数组的命令,只是从一个普通的句子中取出最后一个逗号(差不多)(但是从一个数组中删除了所以不容易改变)。

我想这样做,因为我想将它放在MySQL表中,但是使用最后一个逗号,我无法执行此操作。

2 个答案:

答案 0 :(得分:4)

如果您不想rtirmimplode,那么您可以使用此代码段

<?php
$Title = "aa, bb, cc, dd, ee, ff, gg, hh, ii, jj,";
echo substr($Title, 0, -1);

?>

答案 1 :(得分:3)

使用ncol = 1 lgwidth_old = 0. renderer = fig.canvas.get_renderer() while True: lg = ax.legend(loc='upper center', bbox_to_anchor=(0., -0.05, 1., 0.), borderaxespad=0, ncol=ncol) fig.canvas.draw() lgbbox = lg.get_window_extent(renderer).inverse_transformed(ax.transAxes) if lgwidth_old == lgbbox.width: # All the entries fit within a single row. Keep the legend # as is and break the loop. break if lgbbox.width < 1: # The width of the legend is still smaller than that of the axe. # Continue iterating. ncol += 1 lgwidth_old = lgbbox.width else: # The width of the legend is larger than that of the axe. # Backtrack ncol, plot the legend so it span the entire width of # the axe, and break the loop. ncol -= 1 lg = ax.legend(loc='upper center', bbox_to_anchor=(0., -0.05, 1., 0.), borderaxespad=0, ncol=ncol, mode='expand') break if ncol > 100: print('Number max of iteration reached. Something is wrong.') break 功能

rtrim

第二个参数表示要删除的字符。

尝试

rtrim($Title,',');

输出

$Title = "aa, bb, cc, dd, ee, ff, gg, hh, ii, jj,";
$Title = rtrim($Title,',');
echo $Title;

注意 aa, bb, cc, dd, ee, ff, gg, hh, ii, jj 不仅适用于数组,也不适用于rtirm