Word based cutting text to a given length
- July 23rd, 2007
- php, Snippet-library
This little function cuts a string to a specified length while keeping the word boundaries under consideration.
This little function cuts a string to a specified length while keeping the word boundaries under consideration. <code class=”prettyprint”> function cuttext($text, $chars = 100) { if (strlen($text) > $chars) { $newtext = substr($text, 0, $chars); return preg_replace ("{\s+\S+$}", "...", $newtext); } else { return $text; } } </code>
















Submit your comment