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