note 83585 modified in function.substr by danbrown
| From: | danbrown@php.net | Date: | Fri, 28 Nov 2008 20:15:01 +0000 |
| Subject: | note 83585 modified in function.substr by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-147194@lists.php.net to get a copy of this message | ||
easy and quick way to limit length of a text by not cutting full words:
textLimit('some words', 7) is 'some...'
<?php
function textLimit($string, $length, $replacer = '...')
{
if(strlen($string) > $length)
return (preg_match('/^(.*)\W.*$/', substr($string, 0, $length+1), $matches) ?
$matches[1] : substr($string, 0, $length)) . $replacer;
return $string;
}
?>
--was--
easy and quick way to limit length of a text by not cutting full words:
textLimit('some words', 7) is 'some...'
function textLimit($string, $length, $replacer = '...')
{
if(strlen($string) > $length)
return (preg_match('/^(.*)\W.*$/', substr($string, 0, $length+1), $matches) ?
$matches[1] : substr($string, 0, $length)) . $replacer;
return $string;
}
http://php.net/manual/en/function.substr.php