- substr: documentation ( source)
- strrpos: documentation ( source)
<?php
function truncate($text, $max) {
if (strlen($text) >= $max) {
$text = ereg_replace("<[^>]*>", "", $text);
$text = substr($text, 0, $max);
$position = strrpos($text, " ");
$text = substr($text, 0, $position)."...";
}
return $text;
}
echo truncate('Lorem ipsum dolor <span>sit</span> amet !', 25);
?>