Bolt\Helpers\Html::trimText PHP Method

trimText() public static method

Trim text to a given length.
public static trimText ( string $str, integer $desiredLength, boolean $hellip = true, integer $cutOffCap = 10 ) : string
$str string String to trim
$desiredLength integer Target string length
$hellip boolean Add dots when the string is too long
$cutOffCap integer Maximum difference between string length when removing words
return string Trimmed string
    public static function trimText($str, $desiredLength, $hellip = true, $cutOffCap = 10)
    {
        if ($hellip) {
            $ellipseStr = ' …';
            $newLength = $desiredLength - 1;
        } else {
            $ellipseStr = '';
            $newLength = $desiredLength;
        }
        $str = trim(strip_tags($str));
        if (mb_strlen($str) > $desiredLength) {
            $nextChar = mb_substr($str, $newLength, 1);
            $str = mb_substr($str, 0, $newLength);
            if ($nextChar != ' ') {
                if (false !== ($lastSpace = mb_strrpos($str, ' '))) {
                    // Check for to long cutoff
                    if (mb_strlen($str) - $lastSpace >= $cutOffCap) {
                        // Trim the ellipse, as we do not want a space now
                        return $str . trim($ellipseStr);
                    }
                    $str = mb_substr($str, 0, $lastSpace);
                }
            }
            $str .= $ellipseStr;
        }
        return $str;
    }

Usage Example

Example #1
0
 /**
  * Get the excerpt of a given piece of text.
  *
  * @param int               $length
  * @param bool              $includeTitle
  * @param array|string|null $focus
  *
  * @return string|null
  */
 public function getExcerpt($length = 200, $includeTitle = false, $focus = null)
 {
     $title = null;
     if ($includeTitle && $this->title !== null) {
         $title = Html::trimText(strip_tags($this->title), $length);
         $length = $length - strlen($title);
     }
     if ($this->body instanceof Content) {
         $this->body = $this->body->getValues();
     }
     if (is_array($this->body)) {
         // Assume it's an array, strip some common fields that we don't need, implode the rest.
         $stripKeys = ['id', 'slug', 'datecreated', 'datechanged', 'username', 'ownerid', 'title', 'contenttype', 'status', 'taxonomy', 'templatefields'];
         foreach ($stripKeys as $key) {
             unset($this->body[$key]);
         }
         $excerpt = implode(' ', $this->body);
     } elseif (is_string($this->body) || is_object($this->body) && method_exists($this->body, '__toString')) {
         // otherwise we just use the string.
         $excerpt = (string) $this->body;
     } else {
         // Nope, got nothing.
         $excerpt = '';
     }
     $excerpt = str_replace('>', '> ', $excerpt);
     if (empty($focus)) {
         $excerpt = Html::trimText(strip_tags($excerpt), $length);
     } else {
         $excerpt = $this->extractRelevant($focus, strip_tags($excerpt), $length);
     }
     if ($title !== null) {
         $excerpt = '<b>' . $title . '</b> ' . $excerpt;
     }
     return $excerpt;
 }
All Usage Examples Of Bolt\Helpers\Html::trimText