PHP how to remove and add commas

How to remove extra commas and add commas, for example using PHP

User Submitted Data

stack,,,,,,,,,,,,,,overflow

should be displayed.

stack, overflow
+3
source share
3 answers
$out = preg_replace("/,+/",", ",$in);
+5
source

preg_replace('/[,]+/', ',', $string); should do it ... although depending on the nature of the input, you may need a more complex expression.

+1
source

$str = preg_replace('/,+/' , ',' , $str);

This will replace each sequence of one or more commas with one comma.

0
source

Source: https://habr.com/ru/post/1758828/


All Articles