Skip to content

Instantly share code, notes, and snippets.

@vyspiansky
Last active January 4, 2020 15:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vyspiansky/6e15711426c2183b0f979bfa649ede65 to your computer and use it in GitHub Desktop.
Save vyspiansky/6e15711426c2183b0f979bfa649ede65 to your computer and use it in GitHub Desktop.
PHP: truncate the text without breaking the words into pieces
<?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