Skip to content

Instantly share code, notes, and snippets.

@winsoor
Created October 24, 2017 23:31
Show Gist options
  • Save winsoor/1e5cbc7da85fceba202341ccd61314a5 to your computer and use it in GitHub Desktop.
Save winsoor/1e5cbc7da85fceba202341ccd61314a5 to your computer and use it in GitHub Desktop.
Wordpress
function truncate_string($input_text, $limit = 4, $end_str = '...')
{
$input_text = strip_tags($input_text);
$words = explode(' ', $input_text); // создаём из строки массив слов
if ($limit < 1 || sizeof($words) <= $limit) { // если лимит указан не верно или количество слов меньше лимита, то возвращаем исходную строку
return $input_text;
}
$words = array_slice($words, 0, $limit); // укорачиваем массив до нужной длины
$out = implode(' ', $words);
return $out . $end_str; //возвращаем строку + символ/строка завершения
}
@biswajit-paul
Copy link

You can use Wordpress function wp_trim_words.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment