Skip to content

Instantly share code, notes, and snippets.

@antom
Last active June 7, 2024 11:36
Show Gist options
  • Save antom/661626 to your computer and use it in GitHub Desktop.
Save antom/661626 to your computer and use it in GitHub Desktop.
Returns a string list with the final instance of $glue found substituted with $last - eg. "a, b, c, d, e" -> "a, b, c, d and e" if glue is ', ' and $last is ' and '.
function str_list_ended($value, $glue = ', ', $last = ' and ') {
return preg_replace(
"/($glue)(?!.+$glue)/",
$last,
(is_array($value) ? implode($glue, $value) : $value)
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment