note 53560 added to function.substr
| From: | jay at osu1 dot php dot net | Date: | Mon, 06 Jun 2005 06:26:40 +0000 |
| Subject: | note 53560 added to function.substr | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-89985@lists.php.net to get a copy of this message | ||
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.
----
Manual Page -- http://www.php.net/manual/en/function.substr.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+53560
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+53560&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+53560&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+53560&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+53560&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+53560&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+53560&report=yes
Search -- http://master.php.net/manage/user-notes.php