Created
April 29, 2020 21:38
-
-
Save ethicka/d5b3a309539052c83bb608a349d5a105 to your computer and use it in GitHub Desktop.
Build a comma separated string of one or more strings
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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