note 53560 deleted from function.substr by bobby

From: Date: Thu, 14 Sep 2006 17:25:49 +0000
Subject: note 53560 deleted from function.substr by bobby
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-117179@lists.php.net to get a copy of this message
Note Submitter: jay ---- A more sophisticated ellipse function that lets you end only at the end of words instead of breaking a word to print an ellipse, can however change it using the switch at the end (1/0). This could probably be written better, my PHP skills aren't amazing yet like some others :p <?php // example use: // elliStr("have a nice day",8,1); // results: // have a nice... // // elliStr("have a nice day",8,0); // results: // have a n... function elliStr($s,$n,$e) { if ( isset($e) ) { if ( $e == 1 ) { if ( substr(substr($s, 0, $n), -1, 0) != " " ) { for ( $x = 0; $x <= strlen($s); $x++ ) { if ( substr($s, strlen(substr($s, 0, $n))+$x-1, 1) == " " ) { $newendval = $x; $stop = 1; } else { print(substr(substr($s, 0, $n), strlen(substr($s, 0, $n)), $x)); } if ( isset($stop) ) { return substr($s, 0, $n+$x-1) . "..."; } } } else { return substr($s, 0, $n) . "..."; } } else { if (strlen($s) > $n) { return substr($s, 0, $n) . "..."; } else { return $s; } } } else { return false; } } ?> Hope this helps.

« previous php.notes (#117179) next »