public function viewMore($string, $moreLength = null, $maxLength = null, $lessLength = null, $nl2br = true) { if (!is_numeric($moreLength) || $moreLength <= 0) { $moreLength = $this->_moreLength; } if (!is_numeric($maxLength) || $maxLength <= 0) { $maxLength = $this->_maxLength; } if (!is_numeric($lessLength) || $lessLength <= 0) { $lessLength = $this->_lessLength; } // If using line breaks, ensure that there are not too many line breaks if ($nl2br) { $string = trim(preg_replace('/[\\r\\n]+/', "\n", $string)); if (($c = substr_count($string, "\n")) > $this->_maxLineBreaks) { $pos = 0; for ($i = 0; $i < $this->_maxLineBreaks; $i++) { $pos = strpos($string, "\n", $pos + 1); } if ($pos <= 0 || !is_int($pos)) { $pos = null; } if ($pos) { $moreLength = $pos; } } } // If length is less than max len, just return $strLen = Khcn_String::strlen($string); if ($strLen <= $moreLength + $this->_fudgesicles) { if ($nl2br) { return nl2br($string); } else { return $string; } } // Otherwise truncate if ($strLen >= $maxLength) { $strLen = $maxLength; $string = Khcn_String::substr($string, 0, $maxLength) . $this->view->translate('... '); } $shortText = Khcn_String::substr($string, 0, $moreLength); $fullText = $string; // Do nl2br if ($nl2br) { $shortText = nl2br($shortText); $fullText = nl2br($fullText); } $content = '<' . $this->_tag . ' class="view_more"' . '>' . $shortText . $this->view->translate('... ') . '<a class="view_more_link" href="javascript:void(0);" onclick="$(this).getParent().getNext().style.display=\'\';$(this).getParent().style.display=\'none\';">' . $this->view->translate('more') . '</a>' . '</' . $this->_tag . '>' . '<' . $this->_tag . ' class="view_more"' . ' style="display:none;"' . '>' . $fullText . ' '; if ($strLen >= $lessLength) { $content .= '<a class="view_less_link" href="javascript:void(0);" onclick="$(this).getParent().getPrevious().style.display=\'\';$(this).getParent().style.display=\'none\';">' . $this->view->translate('less') . '</a>'; } $content .= '</' . $this->_tag . '>'; return $content; }
public function truncate($string, $length = 300, $chopString = null) { if (Khcn_String::strlen($string) <= $length) { return $string; } if (null === $chopString) { $chopString = '...'; } $chopString = $this->view->translate($chopString); return Khcn_String::substr($string, 0, $length) . $chopString; }