News

Aug 10, 2011
Category: General
Posted by: dbemowsk
PHP Web Scripting now supports CMS Made Simple.  A clean, robust and flexible content management system that can serve all your website needs.  Let PHP Web Scripting set up your CMS site today.
 
07/11-11 at 13.10 by: dbemowsk
A useful PHP conjunctions function.

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.

No comments registered
Written by:
Email:
Comment:
Write the chars you see in the square
This is a captcha-picture. It is used to prevent mass-access by robots. (see: www.captcha.net)
 
Previous page: Portfolio  Next page: PHP Programming Articles