Add function createTextSnippet() for shortening text to an expected
length Close #1
This commit is contained in:
parent
711bd49048
commit
43bc39d425
22
MD_STD.php
22
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;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user