If you want to add a comma (,) on every word of a sentence, you need to use explode() and implode().

Check the full Code here:

<?php 

// Assuming the text is "Hello World"
$string_sample = "Hello World";

// First, covert the text to array from every space
// Output : array(Hello, World)
$string_to_array = explode(" ", $string_sample);

// Lastly, Convert back the array to text, add comma separated on first parameter
$array_to_string_comma_separated = implode(", ", $string_to_array);

echo $array_to_string_comma_separated;
// output :  Hello, World

?>

This is the same process if you want to achieve the following:

  • Add break between every word
  • Add separator between every word
  • Add HTML element between every word
ADVERTISEMENT

Leave a Comment

Subscribe to the Newsletter

Subscribe to get my latest content by email. I won't send you spam. Unsubscribe at any time.

Newsletter
ADVERTISEMENT

Similar Searches:

  • How to add comma separated on String
  • Covert string to array and add comma on each word
  • Add comma between every word using PHP
  • Add comma between every word programatically
  • Add comma between every word using implode and explode