Skip to content

Instantly share code, notes, and snippets.

@ethicka
Created April 29, 2020 21:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ethicka/d5b3a309539052c83bb608a349d5a105 to your computer and use it in GitHub Desktop.
Save ethicka/d5b3a309539052c83bb608a349d5a105 to your computer and use it in GitHub Desktop.
Build a comma separated string of one or more strings
<?php
/**
* Build Comma-Separated String
* Won't add a hanging comma if there's only passed value
*/
function comma_separated($array) {
$string = implode(', ', $array);
if (preg_match("/, $/", $string)) { // if it ends in a comma, remove the comma
$string = substr($string, 0, -2);
}
return $string;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment