【问题标题】:Removing excess comma in PHP output string删除 PHP 输出字符串中多余的逗号
【发布时间】:2017-02-23 04:24:18
【问题描述】:

我想删除括号内输出字符串中多余的逗号。

 $data= "item_id IN ( '1','2',) AND nt_id IN ( 'er1','er2',) AND";

我使用 rtrim 函数删除了多余的“AND”。

$trimValues = rtrim($data,'AND') ;

但是如何删除括号内的逗号?

【问题讨论】:

    标签: php regex character-trimming


    【解决方案1】:
    ,(?=\s*\))
    

    您可以使用它并替换为empty string。参见演示。

    https://regex101.com/r/rkDV4X/1

    $re = '/,(?=\s*\))/';
    $str = 'item_id IN ( \'1\',\'2\',) AND nt_id IN ( \'er1\',\'er2\',) AND';
    $subst = '';
    
    $result = preg_replace($re, $subst, $str);
    
    echo "The result of the substitution is ".$result;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-29
      • 1970-01-01
      • 1970-01-01
      • 2012-03-09
      • 2014-11-27
      • 2015-07-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多