How to change list separator?

$variable = 'one, two, three';

How to replace commas between words <br>?

$variable should become:

one<br>
two<br>
three
+5
source share
5 answers

Use str_replace:

$variable = str_replace(", ", "<br>", $variable);

or, if you want to do other things with elements between them, explode()and implode():

$variable_exploded = explode(", ", $variable);
$variable_imploded = implode("<br>", $variable_exploded);
+12
source
$variable = str_replace(", ","<br>\n",$variable);

Gotta do the trick.

+8
source
$variable = explode(', ',$variable);
$variable = implode("<br/>\n",$variable);

Then you can just echo $variable

+5
source

You can do:

$variable = str_replace(', ',"<br>\n",$variable);
+3
source
$variable = preg_replace('/\s*,\s*/', "<br>\n", $variable);

This will lead you to a land of regular expressions, but it will handle cases of random comma spacing, for example.

$variable = 'one,two, three';

or

$variable = 'one , two, three';
+3
source

All Articles