Function Reference: geodir_excerpt
Summary
Truncates the text.
Description
Cuts a string to the length of $length and replaces the last characters
with the ellipsis if the text is longer than length.
Package
GeoDirectory
Parameters
- $text
- (string) (required) String to truncate.
- Default: None
- $length
- (int) (required) Length of returned string, including ellipsis.
- Default: None
- $options
- (array) (required) {
An array of HTML attributes and options. @type string $ellipsis Will be used as ending and appended to the trimmed string. Ex: “. “. @type bool $exact If false, $text will not be cut mid-word. @type bool $html If true, HTML tags would be handled correctly. @type bool $trimWidth If true, $text will be truncated with the width. }.- Default: None
Return Values
- (string)
- Trimmed string.
Change Log
Since: 1.6.16
Source File
geodir_excerpt() is located in geodirectory-functions/helper_functions.php [Line: 728]
Source Code
function geodir_excerpt($text, $length = 100, $options = array()) { if (!(int)$length > 0) { return $text; } $default = array( 'ellipsis' => '', 'exact' => true, 'html' => true, 'trimWidth' => false, ); if (!empty($options['html']) && function_exists('mb_internal_encoding') && strtolower(mb_internal_encoding()) === 'utf-8') { $default['ellipsis'] = ""; } $options += $default; $prefix = ''; $suffix = $options['ellipsis']; if ($options['html']) { $ellipsisLength = geodir_strlen(strip_tags($options['ellipsis']), $options); $truncateLength = 0; $totalLength = 0; $openTags = array(); $truncate = ''; preg_match_all('/(<\/?([\w+]+)[^>]*>)?([^<>]*)/', $text, $tags, PREG_SET_ORDER); foreach ($tags as $tag) { $contentLength = geodir_strlen($tag[3], $options); if ($truncate === '') { if (!preg_match('/img|br|input|hr|area|base|basefont|col|frame|isindex|link|meta|param/i', $tag[2])) { if (preg_match('/<[\w]+[^>]*>/', $tag[0])) { array_unshift($openTags, $tag[2]); } elseif (preg_match('/<\/([\w]+)[^>]*>/', $tag[0], $closeTag)) { $pos = array_search($closeTag[1], $openTags); if ($pos !== false) { array_splice($openTags, $pos, 1); } } } $prefix .= $tag[1]; if ($totalLength + $contentLength + $ellipsisLength > $length) { $truncate = $tag[3]; $truncateLength = $length - $totalLength; } else { $prefix .= $tag[3]; } } $totalLength += $contentLength; if ($totalLength > $length) { break; } } if ($totalLength <= $length) { return $text; } $text = $truncate; $length = $truncateLength; foreach ($openTags as $tag) { $suffix .= '' . $tag . '>'; } } else { if (geodir_strlen($text, $options) <= $length) { return $text; } $ellipsisLength = geodir_strlen($options['ellipsis'], $options); } $result = geodir_substr($text, 0, $length - $ellipsisLength, $options); if (!$options['exact']) { if (geodir_substr($text, $length - $ellipsisLength, 1, $options) !== ' ') { $result = geodir_remove_last_word($result); } // Do not need to count ellipsis in the cut, if result is empty. if (!strlen($result)) { $result = geodir_substr($text, 0, $length, $options); } } return $prefix . $result . $suffix; }