Conditions | 19 |
Paths | 194 |
Total Lines | 107 |
Code Lines | 62 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 1 | Features | 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 |
||
54 | public static function truncateHtml($text, $length = 100, $ending = '...', $exact = false, $considerHtml = true): string |
||
55 | { |
||
56 | if ($considerHtml) { |
||
57 | // if the plain text is shorter than the maximum length, return the whole text |
||
58 | if (\mb_strlen( |
||
59 | \preg_replace('/<.*?' . '>/', '', $text) |
||
60 | ) <= $length) { |
||
61 | return $text; |
||
62 | } |
||
63 | // splits all html-tags to scanable lines |
||
64 | \preg_match_all( |
||
65 | '/(<.+?' . '>)?([^<>]*)/s', |
||
66 | $text, |
||
67 | $lines, |
||
68 | \PREG_SET_ORDER |
||
69 | ); |
||
70 | $total_length = \mb_strlen($ending); |
||
71 | $open_tags = []; |
||
72 | $truncate = ''; |
||
73 | foreach ($lines as $line_matchings) { |
||
74 | // if there is any html-tag in this line, handle it and add it (uncounted) to the output |
||
75 | if (!empty($line_matchings[1])) { |
||
76 | // if it's an "empty element" with or without xhtml-conform closing slash |
||
77 | if (\preg_match( |
||
78 | '/^<(\s*.+?\/\s*|\s*(img|br|input|hr|area|base|basefont|col|frame|isindex|link|meta|param)(\s.+?)?)>$/is', |
||
79 | $line_matchings[1] |
||
80 | )) { |
||
81 | // do nothing |
||
82 | // if tag is a closing tag |
||
83 | } elseif (\preg_match('#^<\s*\/(\S+?)\s*>$#s', $line_matchings[1], $tag_matchings)) { |
||
84 | // delete tag from $open_tags list |
||
85 | $pos = \array_search($tag_matchings[1], $open_tags, true); |
||
86 | if (false !== $pos) { |
||
87 | unset($open_tags[$pos]); |
||
88 | } |
||
89 | // if tag is an opening tag |
||
90 | } elseif (\preg_match('/^<\s*([^\s>!]+).*?' . '>$/s', $line_matchings[1], $tag_matchings)) { |
||
91 | // add tag to the beginning of $open_tags list |
||
92 | \array_unshift( |
||
93 | $open_tags, |
||
94 | \mb_strtolower($tag_matchings[1]) |
||
95 | ); |
||
96 | } |
||
97 | // add html-tag to $truncate'd text |
||
98 | $truncate .= $line_matchings[1]; |
||
99 | } |
||
100 | // calculate the length of the plain text part of the line; handle entities as one character |
||
101 | $content_length = \mb_strlen( |
||
102 | \preg_replace('/&[0-9a-z]{2,8};|&#\d{1,7};|[0-9a-f]{1,6};/i', ' ', $line_matchings[2]) |
||
103 | ); |
||
104 | if ($total_length + $content_length > $length) { |
||
105 | // the number of characters which are left |
||
106 | $left = $length - $total_length; |
||
107 | $entities_length = 0; |
||
108 | // search for html entities |
||
109 | if (\preg_match_all( |
||
110 | '/&[0-9a-z]{2,8};|&#\d{1,7};|[0-9a-f]{1,6};/i', |
||
111 | $line_matchings[2], |
||
112 | $entities, |
||
113 | \PREG_OFFSET_CAPTURE |
||
114 | )) { |
||
115 | // calculate the real length of all entities in the legal range |
||
116 | foreach ($entities[0] as $entity) { |
||
117 | if ($left >= $entity[1] + 1 - $entities_length) { |
||
118 | $left--; |
||
119 | $entities_length += \mb_strlen($entity[0]); |
||
120 | } else { |
||
121 | // no more characters left |
||
122 | break; |
||
123 | } |
||
124 | } |
||
125 | } |
||
126 | $truncate .= \mb_substr($line_matchings[2], 0, $left + $entities_length); |
||
127 | // maximum lenght is reached, so get off the loop |
||
128 | break; |
||
129 | } |
||
130 | $truncate .= $line_matchings[2]; |
||
131 | $total_length += $content_length; |
||
132 | // if the maximum length is reached, get off the loop |
||
133 | if ($total_length >= $length) { |
||
134 | break; |
||
135 | } |
||
136 | } |
||
137 | } else { |
||
138 | if (\mb_strlen($text) <= $length) { |
||
139 | return $text; |
||
140 | } |
||
141 | $truncate = \mb_substr($text, 0, $length - \mb_strlen($ending)); |
||
142 | } |
||
143 | // if the words shouldn't be cut in the middle... |
||
144 | if (!$exact) { |
||
145 | // ...search the last occurance of a space... |
||
146 | $spacepos = \mb_strrpos($truncate, ' '); |
||
147 | if (isset($spacepos)) { |
||
148 | // ...and cut the text in this position |
||
149 | $truncate = \mb_substr($truncate, 0, $spacepos); |
||
150 | } |
||
151 | } |
||
152 | // add the defined ending to the text |
||
153 | $truncate .= $ending; |
||
154 | if ($considerHtml) { |
||
155 | // close all unclosed html-tags |
||
156 | foreach ($open_tags as $tag) { |
||
157 | $truncate .= '</' . $tag . '>'; |
||
158 | } |
||
159 | } |
||
160 | return $truncate; |
||
161 | } |
||
222 |