Last active
January 4, 2020 15:56
-
-
Save vyspiansky/6e15711426c2183b0f979bfa649ede65 to your computer and use it in GitHub Desktop.
PHP: truncate the text without breaking the words into pieces
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$charset = 'UTF-8'; | |
$text = 'Повість — літературний прозовий твір середньої форми, довший за оповідання, але коротший за роман, під відповідною назвою поширений у слов\'янських мовах та країнах.'; | |
// truncate a long line to the required length | |
$truncated_text = mb_substr($text, 0, mb_strpos($text, ' ', 35, $charset), $charset); | |
echo $truncated_text; | |
// Output: Повість — літературний прозовий твір |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment