note 81165 added to function.explode

From: Date: Sat, 16 Feb 2008 10:35:41 +0000
Subject: note 81165 added to function.explode
Groups: php.notes 
Request: Send a blank email to php-notes+get-136201@lists.php.net to get a copy of this message
I wrote this function to shorten long names. Actually database has been designed to hold Full name up to 20 characters but some user name had more than 20 chars. /** * Function abbreviate * return abbreviate name * This function is to abbreviate the name. If name is grater than specific length * then it abbreviate the string * * * Parameters: * limit - The maximum character count * string - The name string * * Returns: * string - abbreviate string */ function abbreviate($limit, $string, $position=0) { $char_limit=$limit; $string = trim($string); $str_chunk = explode(' ', $string); if(strlen($string) > $char_limit) { if(sizeof($str_chunk) == ($position+1)){ return $string = substr($string, 0, $char_limit); } else { $str_chunk[$position] = substr($str_chunk[$position], 0, 1).'.'; return abbrivate($char_limit,implode(' ',$str_chunk), ++$position); } } else { return $string; } } //Usage $name = 'Abhinav Varshney This Is My Name'; $char_limit=29; echo abbreviate($char_limit, $name); ---- Server IP: 203.199.69.133 Probable Submitter: 202.164.58.81 ---- Manual Page -- http://www.php.net/manual/en/function.explode.php Edit -- https://master.php.net/note/edit/81165 Del: integrated -- https://master.php.net/note/delete/81165/integrated Del: useless -- https://master.php.net/note/delete/81165/useless Del: bad code -- https://master.php.net/note/delete/81165/bad+code Del: spam -- https://master.php.net/note/delete/81165/spam Del: non-english -- https://master.php.net/note/delete/81165/non-english Del: in docs -- https://master.php.net/note/delete/81165/in+docs Del: other reasons-- https://master.php.net/note/delete/81165 Reject -- https://master.php.net/note/reject/81165 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#136201) next »