In my dealings with PHP, there have been times when I have had the need to create conjoined text strings. These are words that are connected with a conjunction. the most widely used of these are "and", "but" and "or". Some examples would be: horse, pig, chicken and cow book, magazine, or newspaper I have written a simple little function that will take an array of words or phrases and conjoin them together to form a conjoined string like the samples above.
/**
* @ conjunct - This takes an array list of words and conjoins the list of words by removing
* the last word from the list and inserting the conjunction to form the conjoined
* array of words into a string.
*
* @ param type: array $wordsArray = An array of two or more words to form a conjunction
* @ param type: text $conjunction = The conjunction word used to join the list of words
* @ param type: boolean $serialComma = Tells whether or not to use the serial comma (series comma)
*
* @ return: The formatted conjoined string.
*/
function conjunct($wordsArray, $conjunction="and", $serialComma=true) {
$lastWord = array_pop($wordsArray);
if (count($wordsArray) > 1)
$ret = implode(", ", $wordsArray) . (($serialComma) ? "," : "") . " $conjunction $lastWord";
else
$ret = $lastWord; return $ret;
}
Though simple, this is one little function I always keep handy. I hope this little snippet is useful to some of you out there.