Convert comma separated text to new line in php
You can use the explode()
function in PHP to convert a comma-separated string to an array, and then use a loop to convert each element of the array to a new line.
Here is an example:
$string = "apple,banana,orange,grape";
// Convert to array
$array = explode(",", $string);
// Convert array to new lines
$new_string = "";
foreach ($array as $item) {
$new_string.= $item. "\n";
}
echo $new_string;
This will output:
apple
banana
orange
grape
Alternatively, you can use the implode()
function to convert the array to a string with new lines:
$string = "apple,banana,orange,grape";
// Convert to array
$array = explode(",", $string);
// Convert array to new lines
$new_string = implode("\n", $array);
echo $new_string;
This will also output:
apple
banana
orange
grape
You can also use str_replace()
function to replace comma with new line:
$string = "apple,banana,orange,grape";
$new_string = str_replace(",", "\n", $string);
echo $new_string;
This will also output:
apple
banana
orange
grape