Created
October 24, 2017 23:31
-
-
Save winsoor/1e5cbc7da85fceba202341ccd61314a5 to your computer and use it in GitHub Desktop.
Wordpress
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
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; //возвращаем строку + символ/строка завершения | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can use Wordpress function wp_trim_words.