<?php $strings = [ 'StackOverFlow is the best site ever', // 22 ----------------^ 'http://stackoverflow.com is the best site ever', // 22 ----------------^ 'Short string', // no ellipsis needed ^ 'pneumonoultramicroscopicsilicovolcanoconiosis', // breakpoint between ^ letters ]; $link = '<a href="/this/story">Read More</a>'; function ShortenText($text, $chars) { $chars = $chars;$text = $text." "; $countchars = strlen($text); if($countchars > $chars) { $text = substr($text,0,$chars); $text = substr($text,0,strrpos($text,' ')); $text = $text."..."; } return $text; } foreach ($strings as $string) { var_export( ShortenText($string, 22) ); echo "\n"; }
You have javascript disabled. You will not be able to edit any code.