Shakun Chaudhary Shakun Chaudhary - 3 months ago 7
PHP Question

Remove comma from string in php

suppose the value is

1,2,,4,5,6
- For this i can use
str_replace(",,",",",$mystring)
to get
1,2,4,5,6


How to get values like
2,4,5,6
from
,2,,4,5,6
where two or more consecutive commas are replaced by a single comma and if comma comes before any value it is neglected.If there is only commas like
,,,,,,
then empty value is returned.

how to do this in php.

Answer
//Try this: 

$str = "1,2,,4,5,6";
$formattedString = implode(',', array_filter(explode(',', $str)) );
Comments