diff --git a/MD_STD.php b/MD_STD.php index 3c761b9..262721c 100644 --- a/MD_STD.php +++ b/MD_STD.php @@ -356,4 +356,26 @@ final class MD_STD { } + /** + * This function cuts down a string and adds a period in case it's longer + * than length to create a snippet. + * + * @param string $string Input text to cut down. + * @param integer $length Length of the snippet to create. + * + * @return string + */ + public static function createTextSnippet(string $string, int $length = 180):string { + + if (\mb_strlen($string) > $length) { + $string = \mb_substr($string, 0, $length); + if (($lastWhitespace = \mb_strrpos($string, ' ')) !== false) { + $string = \mb_substr($string, 0, $lastWhitespace); + } + $string .= '...'; + } + return $string; + + } + }