| Conditions | 19 |
| Paths | 194 |
| Total Lines | 91 |
| Code Lines | 49 |
| 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 |
||
| 193 | public static function truncateHtml($text, $length = 100, $ending = '...', $exact = false, $considerHtml = true) |
||
| 194 | { |
||
| 195 | $openTags = []; |
||
| 196 | if ($considerHtml) { |
||
| 197 | // if the plain text is shorter than the maximum length, return the whole text |
||
| 198 | if (strlen(preg_replace('/<.*?' . '>/', '', $text)) <= $length) { |
||
| 199 | return $text; |
||
| 200 | } |
||
| 201 | // splits all html-tags to scanable lines |
||
| 202 | preg_match_all('/(<.+?' . '>)?([^<>]*)/s', $text, $lines, PREG_SET_ORDER); |
||
| 203 | $total_length = strlen($ending); |
||
| 204 | //$openTags = []; |
||
| 205 | $truncate = ''; |
||
| 206 | foreach ($lines as $line_matchings) { |
||
| 207 | // if there is any html-tag in this line, handle it and add it (uncounted) to the output |
||
| 208 | if (!empty($line_matchings[1])) { |
||
| 209 | // if it's an "empty element" with or without xhtml-conform closing slash |
||
| 210 | if (preg_match('/^<(\s*.+?\/\s*|\s*(img|br|input|hr|area|base|basefont|col|frame|isindex|link|meta|param)(\s.+?)?)>$/is', $line_matchings[1])) { |
||
| 211 | // do nothing |
||
| 212 | // if tag is a closing tag |
||
| 213 | } elseif (preg_match('/^<\s*\/([^\s]+?)\s*>$/s', $line_matchings[1], $tag_matchings)) { |
||
| 214 | // delete tag from $openTags list |
||
| 215 | $pos = array_search($tag_matchings[1], $openTags); |
||
| 216 | if (false !== $pos) { |
||
| 217 | unset($openTags[$pos]); |
||
| 218 | } |
||
| 219 | // if tag is an opening tag |
||
| 220 | } elseif (preg_match('/^<\s*([^\s>!]+).*?' . '>$/s', $line_matchings[1], $tag_matchings)) { |
||
| 221 | // add tag to the beginning of $openTags list |
||
| 222 | array_unshift($openTags, strtolower($tag_matchings[1])); |
||
| 223 | } |
||
| 224 | // add html-tag to $truncate'd text |
||
| 225 | $truncate .= $line_matchings[1]; |
||
| 226 | } |
||
| 227 | // calculate the length of the plain text part of the line; handle entities as one character |
||
| 228 | $content_length = strlen(preg_replace('/&[0-9a-z]{2,8};|&#[0-9]{1,7};|[0-9a-f]{1,6};/i', ' ', $line_matchings[2])); |
||
| 229 | if ($total_length + $content_length > $length) { |
||
| 230 | // the number of characters which are left |
||
| 231 | $left = $length - $total_length; |
||
| 232 | $entities_length = 0; |
||
| 233 | // search for html entities |
||
| 234 | 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)) { |
||
| 235 | // calculate the real length of all entities in the legal range |
||
| 236 | foreach ($entities[0] as $entity) { |
||
| 237 | if ($entity[1] + 1 - $entities_length <= $left) { |
||
| 238 | $left--; |
||
| 239 | $entities_length += strlen($entity[0]); |
||
| 240 | } else { |
||
| 241 | // no more characters left |
||
| 242 | break; |
||
| 243 | } |
||
| 244 | } |
||
| 245 | } |
||
| 246 | $truncate .= substr($line_matchings[2], 0, $left + $entities_length); |
||
| 247 | // maximum lenght is reached, so get off the loop |
||
| 248 | break; |
||
| 249 | } else { |
||
| 250 | $truncate .= $line_matchings[2]; |
||
| 251 | $total_length += $content_length; |
||
| 252 | } |
||
| 253 | // if the maximum length is reached, get off the loop |
||
| 254 | if ($total_length >= $length) { |
||
| 255 | break; |
||
| 256 | } |
||
| 257 | } |
||
| 258 | } else { |
||
| 259 | if (strlen($text) <= $length) { |
||
| 260 | return $text; |
||
| 261 | } else { |
||
| 262 | $truncate = substr($text, 0, $length - strlen($ending)); |
||
| 263 | } |
||
| 264 | } |
||
| 265 | // if the words shouldn't be cut in the middle... |
||
| 266 | if (!$exact) { |
||
| 267 | // ...search the last occurance of a space... |
||
| 268 | $spacepos = mb_strrpos($truncate, ' '); |
||
| 269 | if (isset($spacepos)) { |
||
| 270 | // ...and cut the text in this position |
||
| 271 | $truncate = substr($truncate, 0, $spacepos); |
||
| 272 | } |
||
| 273 | } |
||
| 274 | // add the defined ending to the text |
||
| 275 | $truncate .= $ending; |
||
| 276 | if ($considerHtml) { |
||
| 277 | // close all unclosed html-tags |
||
| 278 | foreach ($openTags as $tag) { |
||
| 279 | $truncate .= '</' . $tag . '>'; |
||
| 280 | } |
||
| 281 | } |
||
| 282 | |||
| 283 | return $truncate; |
||
| 284 | } |
||
| 303 |