/** * Decodes mixed params. * * @param mixed The encoded object/array/string/etc * @return mixed The decoded version */ static function decode_htmlchars(&$data) { if (is_object($data) || is_array($data)) { foreach ($data as $index => &$datum) { //skip any fields with prefix binary_ if (substr($index, 0, 7) != 'binary_') { $datum = ilp_db::decode_htmlchars($datum); } } return $data; } else { return str_replace(array('"', ''', '<', '>'), array('"', "'", '<', '>'), $data); } }
/** * Truncates long strings and adds a tooltip with a longer verison. * * @param string $string The string to truncate * @param int $maxlength The maximum length the string can be. -1 means unlimited, in case you just want a tooltip * @param string $tooltip (optional) tooltip to display. defaults to $string * @param array $special_case (optional) array of characters/entities that if found in string * stop the truncation and deceoding * @return string HTML */ function ilp_limit_length($html, $maxlength, $tooltip = null) { // permit only html tags and quotes so we can parse the tags properly $html = ilp_db::decode_htmlchars(assmgr_db::encode($html)); $printedlength = 0; $position = 0; $tags = array(); $return = null; while ($printedlength < $maxlength && preg_match('{</?([a-z]+)[^>]*>|&#?[a-zA-Z0-9]+;}', $html, $match, PREG_OFFSET_CAPTURE, $position)) { list($tag, $tagPosition) = $match[0]; // print text leading up to the tag $str = substr($html, $position, $tagPosition - $position); if ($printedlength + strlen($str) > $maxlength) { $return .= substr($str, 0, $maxlength - $printedlength); $printedlength = $maxlength; break; } $return .= $str; $printedlength += strlen($str); if ($tag[0] == '&') { // handle the entity $return .= $tag; $printedlength++; } else { // handle the tag $tagName = $match[1][0]; if ($tag[1] == '/') { // this is a closing tag $openingTag = array_pop($tags); assert($openingTag == $tagName); // check that tags are properly nested $return .= $tag; } else { if ($tag[strlen($tag) - 2] == '/') { // self-closing tag $return .= $tag; } else { // opening tag $return .= $tag; $tags[] = $tagName; } } } // continue after the tag $position = $tagPosition + strlen($tag); } // print any remaining text if ($printedlength < $maxlength && $position < strlen($html)) { $return .= substr($html, $position, $maxlength - $printedlength); } // add the ellipsis, if truncated $return .= strip_tags($return) != strip_tags($html) ? '…' : null; // close any open tags while (!empty($tags)) { $return .= sprintf('</%s>', array_pop($tags)); } // don't show a tooltip if it's set to false, or if no truncate has been done if ($tooltip === false || $return == $html && empty($tooltip)) { return $return; } else { // make the tooltip the original string if a specific value was not set if (empty($tooltip)) { $tooltip = $html; } $tooltip = ilp_db::encode($tooltip); // generate the unique id needed for the YUI tooltip $id = 'tootlip' . ilp_uniqueNum(); $script = "<script type='text/javascript'>\r\n //<![CDATA[\r\n new YAHOO.widget.Tooltip('ttA{$id}', {\r\n context:'{$id}',\r\n effect:{effect:YAHOO.widget.ContainerEffect.FADE,duration:0.20}\r\n });\r\n //]]>\r\n </script>"; return "<span id='{$id}' class='tooltip' title='{$tooltip}'>{$return}</span>{$script}"; } }