| Conditions | 19 |
| Paths | 194 |
| Total Lines | 89 |
| Code Lines | 47 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 99 | public static function truncateHtml($text, $length = 100, $ending = '...', $exact = false, $considerHtml = true) |
||
| 100 | { |
||
| 101 | $open_tags = []; |
||
| 102 | if ($considerHtml) { |
||
| 103 | // if the plain text is shorter than the maximum length, return the whole text |
||
| 104 | if (mb_strlen(\preg_replace('/<.*?' . '>/', '', $text)) <= $length) { |
||
| 105 | return $text; |
||
| 106 | } |
||
| 107 | // splits all html-tags to scanable lines |
||
| 108 | \preg_match_all('/(<.+?' . '>)?([^<>]*)/s', $text, $lines, \PREG_SET_ORDER); |
||
| 109 | $total_length = mb_strlen($ending); |
||
| 110 | $truncate = ''; |
||
| 111 | foreach ($lines as $line_matchings) { |
||
| 112 | // if there is any html-tag in this line, handle it and add it (uncounted) to the output |
||
| 113 | if (!empty($line_matchings[1])) { |
||
| 114 | // if it's an "empty element" with or without xhtml-conform closing slash |
||
| 115 | if (\preg_match('/^<(\s*.+?\/\s*|\s*(img|br|input|hr|area|base|basefont|col|frame|isindex|link|meta|param)(\s.+?)?)>$/is', $line_matchings[1])) { |
||
| 116 | // do nothing |
||
| 117 | // if tag is a closing tag |
||
| 118 | } elseif (\preg_match('/^<\s*\/(\S+?)\s*>$/s', $line_matchings[1], $tag_matchings)) { |
||
| 119 | // delete tag from $open_tags list |
||
| 120 | $pos = \array_search($tag_matchings[1], $open_tags, true); |
||
| 121 | if (false !== $pos) { |
||
| 122 | unset($open_tags[$pos]); |
||
| 123 | } |
||
| 124 | // if tag is an opening tag |
||
| 125 | } elseif (\preg_match('/^<\s*([^\s>!]+).*?' . '>$/s', $line_matchings[1], $tag_matchings)) { |
||
| 126 | // add tag to the beginning of $open_tags list |
||
| 127 | \array_unshift($open_tags, mb_strtolower($tag_matchings[1])); |
||
| 128 | } |
||
| 129 | // add html-tag to $truncate'd text |
||
| 130 | $truncate .= $line_matchings[1]; |
||
| 131 | } |
||
| 132 | // calculate the length of the plain text part of the line; handle entities as one character |
||
| 133 | $content_length = mb_strlen(\preg_replace('/&[0-9a-z]{2,8};|&#[0-9]{1,7};|[0-9a-f]{1,6};/i', ' ', $line_matchings[2])); |
||
| 134 | if ($total_length + $content_length > $length) { |
||
| 135 | // the number of characters which are left |
||
| 136 | $left = $length - $total_length; |
||
| 137 | $entities_length = 0; |
||
| 138 | // search for html entities |
||
| 139 | if (\preg_match_all('/&[0-9a-z]{2,8};|&#[0-9]{1,7};|[0-9a-f]{1,6};/i', $line_matchings[2], $entities, \PREG_OFFSET_CAPTURE)) { |
||
| 140 | // calculate the real length of all entities in the legal range |
||
| 141 | foreach ($entities[0] as $entity) { |
||
| 142 | if ($left >= $entity[1] + 1 - $entities_length) { |
||
| 143 | $left--; |
||
| 144 | $entities_length += mb_strlen($entity[0]); |
||
| 145 | } else { |
||
| 146 | // no more characters left |
||
| 147 | break; |
||
| 148 | } |
||
| 149 | } |
||
| 150 | } |
||
| 151 | $truncate .= mb_substr($line_matchings[2], 0, $left + $entities_length); |
||
| 152 | // maximum lenght is reached, so get off the loop |
||
| 153 | break; |
||
| 154 | } |
||
| 155 | $truncate .= $line_matchings[2]; |
||
| 156 | $total_length += $content_length; |
||
| 157 | |||
| 158 | // if the maximum length is reached, get off the loop |
||
| 159 | if ($total_length >= $length) { |
||
| 160 | break; |
||
| 161 | } |
||
| 162 | } |
||
| 163 | } else { |
||
| 164 | if (mb_strlen($text) <= $length) { |
||
| 165 | return $text; |
||
| 166 | } |
||
| 167 | $truncate = mb_substr($text, 0, $length - mb_strlen($ending)); |
||
| 168 | } |
||
| 169 | // if the words shouldn't be cut in the middle... |
||
| 170 | if (!$exact) { |
||
| 171 | // ...search the last occurance of a space... |
||
| 172 | $spacepos = mb_strrpos($truncate, ' '); |
||
| 173 | if (isset($spacepos)) { |
||
| 174 | // ...and cut the text in this position |
||
| 175 | $truncate = mb_substr($truncate, 0, $spacepos); |
||
| 176 | } |
||
| 177 | } |
||
| 178 | // add the defined ending to the text |
||
| 179 | $truncate .= $ending; |
||
| 180 | if ($considerHtml) { |
||
| 181 | // close all unclosed html-tags |
||
| 182 | foreach ($open_tags as $tag) { |
||
| 183 | $truncate .= '</' . $tag . '>'; |
||
| 184 | } |
||
| 185 | } |
||
| 186 | |||
| 187 | return $truncate; |
||
| 188 | } |
||
| 190 |