Skip to content

Instantly share code, notes, and snippets.

@chiliec
Created December 29, 2014 11:39
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 chiliec/53d7cf04c113073d5919 to your computer and use it in GitHub Desktop.
Save chiliec/53d7cf04c113073d5919 to your computer and use it in GitHub Desktop.
Функция сокращения текста (cut) по тегам переноса строки
/**
* Copyright (c) 2013 Vladimir Babin <vovababin@gmail.com>
* Функция для сокращения текста (cut) по тегам переноса строки
* $post - текст для сокращения
* $from - минимальное количество символов
* $until - максимальное количество символов
* $intro - возможность передать текст вступления вручную
* $allowable_tags - разрешенные теги
*/
public function cut($post, $from=300, $until=1000, $intro='', $allowable_tags = '<br>')
{
if($intro=='') {
if(mb_strlen($post, 'utf-8')>=$until) { // если текст больше максимального
$string = mb_substr($post, $from, $until, 'utf-8');
if(!$position = mb_strpos($string, "</p>", 0, 'utf-8'))
if(!$position = mb_strpos($string, "<br>", 0, 'utf-8'))
if(!$position = mb_strpos($string, "<br />", 0, 'utf-8'))
$position = $until;
$result = mb_substr($post, 0, $from+$position, 'utf-8');
} else {
$result = $post;
}
} else {
$result = $intro;
}
strip_tags($result, $allowable_tags); // удаляем лишние теги
return $result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment