Replace 2 commas separated by space with space using php

后端 未结 4 774
故里飘歌
故里飘歌 2021-01-17 06:10

Hi friends am trying to replace two commas separated by space with space using php. Here is my code

Suppose for example am having variable like

 $va         


        
4条回答
  •  鱼传尺愫
    2021-01-17 06:34

    Try this option:

    $var = "hello, welcome, , , , ,";
    $output = preg_replace('/,\s(?=[,])|,$/', '', $var);
    
    hello, welcome
    

    Demo

    This uses a lookahead to target only commas that are followed by a space and a comma, or by the end of the string.

提交回复
热议问题