Skip to content

Instantly share code, notes, and snippets.

@garyconstable
Created October 7, 2019 08:52
Show Gist options
  • Select an option

  • Save garyconstable/c66cd2850c751d89c06900a84d90fe00 to your computer and use it in GitHub Desktop.

Select an option

Save garyconstable/c66cd2850c751d89c06900a84d90fe00 to your computer and use it in GitHub Desktop.

Revisions

  1. garyconstable revised this gist Oct 7, 2019. 1 changed file with 33 additions and 29 deletions.
    62 changes: 33 additions & 29 deletions ellipsis.php
    Original file line number Diff line number Diff line change
    @@ -1,38 +1,42 @@
    <?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
    */
    protected function getExcerpt($text, $strip_tags = true, $max_length = 140, $cut_off = '...', $keep_word = true)
    {
    if ($strip_tags) {
    $text = strip_tags($text);
    }
    * 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) {
    return $text;
    }

    if (strlen($text) > $max_length) {
    if ($keep_word) {
    $text = substr($text, 0, $max_length + 1);
    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);
    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;
    }
    return $text;
    }

    ?>
  2. garyconstable created this gist Oct 7, 2019.
    38 changes: 38 additions & 0 deletions ellipsis.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    /**
    * 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
    */
    protected 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;
    }