Created
April 2, 2014 22:25
-
-
Save kadimi/9944468 to your computer and use it in GitHub Desktop.
Convert an array of words to real text.
This file contains 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
function array_to_real_text (arr) { | |
txt = arr.join(", " ); | |
return txt = str_replace_last(', ', ' and ', txt); | |
} | |
function str_replace_last(needle, replacement, str) { | |
var needle_index = str.lastIndexOf(needle); | |
if (needle_index < 0) { | |
return str; | |
} else { | |
return str.substr(0, needle_index) + replacement + str.substr(needle_index + needle.length); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment