Go Back   CodingForums.com > :: Server side development > PHP > Post a PHP snippet

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-18-2012, 08:55 PM   PM User | #1
BornKillaz
New Coder

 
Join Date: Jun 2012
Location: Switzerland
Posts: 22
Thanks: 7
Thanked 1 Time in 1 Post
BornKillaz is an unknown quantity at this point
Lightbulb readable_implode() - adds the word "and" before the last array element.

Hello.

Here's a simple function of mine, similar to implode(), but with one major difference, it adds the word "and" (or any other) before the last element. Useful for comma separated words.

PHP Code:
/**
 * readable_implode()
 * 
 * @param array $array
 * @param string $and
 * @return string
 */
function readable_implode$array$and 'and' ) {
    
    
$last array_pop$array ) . '.';

    return ( 
count$array ) > ) ? implode', '$array ) . ' ' $and ' ' $last $last;
    

Try it out...
PHP Code:
<?php

$names 
= array( 'Sandra''David''Tim''Rachel' );

// Should output: Sandra, David, Tim and Rachel.
echo readable_implode$names );

?>
Thanks.

Last edited by BornKillaz; 07-18-2012 at 09:04 PM..
BornKillaz is offline   Reply With Quote
Reply

Bookmarks

Tags
readable_implode

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:37 PM.


Advertisement
Log in to turn off these ads.