如何删除last'';字符串的


How to remove last ',' of a string?

我想删除字符串的最后一个",":

字符串是这样的:

$str = a,b,c,d,e

结果应该是:

$str = a,b,c,de

http://php.net/manual/en/function.strrpos.php

strrpos—查找字符串中子字符串最后一次出现的位置

一旦找到最后一个,的位置,就可以删除它,例如使用子字符串操作。

一个很短的方法是

$str  = "a,b,c,d,e";
echo substr_replace( $str, "", strrpos( $str, "," ), 1 );

您可以执行以下

$str = preg_replace(/,(.*)$/U, '$1', $str)

使用preg_replace。我相信您可以为匹配设计一个合适的正则表达式(类似于,([^,])*$的summat)和一个适当的替换