Skip to content

Instantly share code, notes, and snippets.

@dryks
Last active August 30, 2017 13:26
Show Gist options
  • Save dryks/8c7b2760f784a624fc35a709cfd299be to your computer and use it in GitHub Desktop.
Save dryks/8c7b2760f784a624fc35a709cfd299be to your computer and use it in GitHub Desktop.
функция Обрезание по колличеству слов function php word
// функция Обрезание по колличеству слов
<?
function words_limit($input_text, $limit = 50, $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