Thursday, September 10, 2009

PHP function that produces acronym

I wrote this function because I couldn't find one that produces an acronym from a string of words.

Here's the script:

function acronym($longname) {
$letters=array();
$words=explode(' ', $longname);
foreach($words as $word) {
$word = (substr($word, 0, 1));
array_push($letters, $word);
}
$shortname = strtolower(implode($letters));
return $shortname;
}

// call the script like this:

$_POST['title'] = acronym($_POST['title']);

No comments:

Post a Comment

If you want to include code in your comment, use this function first. It's provided by Bogdan Valentin - Thanks Bogdan!