note 39200 added to function.substr-replace
| From: | (thijs\[at\]nllinux.nl) | Date: | Tue, 20 Jan 2004 16:05:45 +0000 |
| Subject: | note 39200 added to function.substr-replace | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-63887@lists.php.net to get a copy of this message | ||
I wrote a function that you can use for example in combination with a search script to cut off the
articles that are too long.
<?php
function substr_index($text, $maxChars = 20, $splitter
= '...') {
$theReturn = $text;
$lastSpace = false;
if (strlen($text) > $maxChars) {
$theReturn = substr($text, 0, $maxChars - 1);
if (in_array(substr($text, $maxChars - 1, 1),
array(' ', '.', '!', '?'))) {
$theReturn .= substr($text, $maxChars, 1);
} else {
$theReturn = substr($theReturn, 0, $maxChars -
strlen($splitter));
$lastSpace = strrpos($theReturn, ' ');
if ($lastSpace !== false) {
$theReturn = substr($theReturn, 0, $lastSpace);
}
if (in_array(substr($theReturn, -1, 1), array(','))) {
$theReturn = substr($theReturn, 0, -1);
}
$theReturn .= $splitter;
}
}
return $theReturn;
}
?>
----
Manual Page -- http://www.php.net/manual/en/function.substr-replace.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+39200
Delete -- http://master.php.net/manage/user-notes.php?action=delete+39200&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+39200&report=yes
Search -- http://master.php.net/manage/user-notes.php