Skip to content

Instantly share code, notes, and snippets.

@garyconstable
Created October 7, 2019 08:52
Show Gist options
  • Save garyconstable/c66cd2850c751d89c06900a84d90fe00 to your computer and use it in GitHub Desktop.
Save garyconstable/c66cd2850c751d89c06900a84d90fe00 to your computer and use it in GitHub Desktop.
Ellipsis (php)
<?php
/**
* Take the landing page content, strip the tags and return an shortened versions.
*
* @param $text
* @param bool $strip_tags
* @param int $max_length
* @param string $cut_off
* @param bool $keep_word
* @return bool|string
*/
function getExcerpt($text, $strip_tags = true, $max_length = 140, $cut_off = '...', $keep_word = true)
{
if ($strip_tags) {
$text = strip_tags($text);
}
if (strlen($text) <= $max_length) {
return $text;
}
if (strlen($text) > $max_length) {
if ($keep_word) {
$text = substr($text, 0, $max_length + 1);
if ($last_space = strrpos($text, ' ')) {
$text = substr($text, 0, $last_space);
$text = rtrim($text);
$text .= $cut_off;
}
} else {
$text = substr($text, 0, $max_length);
$text = rtrim($text);
$text .= $cut_off;
}
}
return $text;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment