Changeset 125264 in spip-zone
- Timestamp:
- Jun 22, 2020, 7:19:34 AM (7 months ago)
- Location:
- _plugins_/spip-bonux/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
_plugins_/spip-bonux/trunk
- Property subgit:lock:23963b88b7a34690d9c6cd86b36c1f6f6a839c51 deleted
-
Property
subgit:lock:cad076fceb3a7e955667879d0d2ba20516235464
set to
2020-06-22T09:25:09.828
-
_plugins_/spip-bonux/trunk/spip_bonux_options.php
r125263 r125264 87 87 if (is_string($valeur) and !is_numeric($valeur)) { 88 88 // Si on est en >=3.2, on peut extraire les <:chaine:> 89 $version = explode('.', 89 $version = explode('.',$GLOBALS['spip_version_branche']); 90 90 $extraction_chaines = (($version[0] > 3 or $version[1] >= 2) ? true : false); 91 91 // Si la chaine est du type <:truc:> on passe à _T() 92 92 if (strpos($valeur, '<:') !== false 93 93 and preg_match('/^\<:([^>]*?):\>$/', $valeur, $match)) { 94 94 $valeur = _T($match[1]); 95 95 } else { … … 99 99 or ($mode_typo === 'multi' and strpos($valeur, '<multi>') !== false) 100 100 or ($extraction_chaines 101 102 103 104 101 and $mode_typo === 'multi' 102 and strpos($valeur, '<:') !== false 103 and include_spip('inc/filtres') 104 and preg_match(_EXTRAIRE_IDIOME, $valeur)) 105 105 ) { 106 106 include_spip('inc/texte'); … … 134 134 */ 135 135 if (!function_exists('spip_array_insert')) { 136 137 138 139 140 141 142 143 144 145 146 147 136 function spip_array_insert($arr1, $cle, $arr2, $avant = false) { 137 $index = array_search($cle, array_keys($arr1)); 138 if ($index === false) { 139 $index = count($arr1); // insert @ end of array if $key not found 140 } else { 141 if (!$avant) { 142 $index++; 143 } 144 } 145 $fin = array_splice($arr1, $index); 146 return array_merge($arr1, $arr2, $fin); 147 } 148 148 } 149 149 … … 186 186 187 187 if (!function_exists('text_truncate')) { 188 /** 189 * Truncates text. 190 * 191 * Cuts a string to the length of $length and replaces the last characters 192 * with the ending if the text is longer than length. 193 * 194 * ### Options: 195 * 196 * - `ending` Will be used as Ending and appended to the trimmed string 197 * - `exact` If false, $text will not be cut mid-word 198 * - `html` If true, HTML tags would be handled correctly 199 * 200 * @param string $text String to truncate. 201 * @param integer $length Length of returned string, including ellipsis. 202 * @param array $options An array of html attributes and options. 203 * @return string Trimmed string. 204 * @access public 205 * @link https://api.cakephp.org/4.0/class-Cake.Utility.Text.html#truncate 206 */ 207 function text_truncate($text, $length = 100, $options = array()) { 208 $default = array( 209 'ending' => '...', 'exact' => true, 'html' => false 210 ); 211 $options = array_merge($default, $options); 212 extract($options); 213 214 if ($html) { 215 if (mb_strlen(preg_replace('/<.*?>/', '', $text)) <= $length) { 216 return $text; 217 } 218 $totalLength = mb_strlen(strip_tags($ending)); 219 $openTags = array(); 220 $truncate = ''; 221 222 preg_match_all('/(<\/?([\w+]+)[^>]*>)?([^<>]*)/', $text, $tags, PREG_SET_ORDER); 223 foreach ($tags as $tag) { 224 if (!preg_match('/img|br|input|hr|area|base|basefont|col|frame|isindex|link|meta|param/s', $tag[2])) { 225 if (preg_match('/<[\w]+[^>]*>/s', $tag[0])) { 226 array_unshift($openTags, $tag[2]); 227 } elseif (preg_match('/<\/([\w]+)[^>]*>/s', $tag[0], $closeTag)) { 228 $pos = array_search($closeTag[1], $openTags); 229 if ($pos !== false) { 230 array_splice($openTags, $pos, 1); 188 /** 189 * Truncates text. 190 * 191 * Cuts a string to the length of $length and replaces the last characters 192 * with the ending if the text is longer than length. 193 * 194 * ### Options: 195 * 196 * - `ending` Will be used as Ending and appended to the trimmed string 197 * - `exact` If false, $text will not be cut mid-word 198 * - `html` If true, HTML tags would be handled correctly 199 * 200 * @param string $text String to truncate. 201 * @param integer $length Length of returned string, including ellipsis. 202 * @param array $options An array of html attributes and options. 203 * @return string Trimmed string. 204 * @access public 205 * @link http://book.cakephp.org/view/1469/Text#truncate-1625 206 */ 207 function text_truncate($text, $length = 100, $options = array()) { 208 $default = array( 209 'ending' => '...', 'exact' => true, 'html' => false 210 ); 211 $options = array_merge($default, $options); 212 extract($options); 213 214 if ($html) { 215 if (mb_strlen(preg_replace('/<.*?>/', '', $text)) <= $length) { 216 return $text; 217 } 218 $totalLength = mb_strlen(strip_tags($ending)); 219 $openTags = array(); 220 $truncate = ''; 221 222 preg_match_all('/(<\/?([\w+]+)[^>]*>)?([^<>]*)/', $text, $tags, PREG_SET_ORDER); 223 foreach ($tags as $tag) { 224 if (!preg_match('/img|br|input|hr|area|base|basefont|col|frame|isindex|link|meta|param/s', $tag[2])) { 225 if (preg_match('/<[\w]+[^>]*>/s', $tag[0])) { 226 array_unshift($openTags, $tag[2]); 227 } else if (preg_match('/<\/([\w]+)[^>]*>/s', $tag[0], $closeTag)) { 228 $pos = array_search($closeTag[1], $openTags); 229 if ($pos !== false) { 230 array_splice($openTags, $pos, 1); 231 } 232 } 233 } 234 $truncate .= $tag[1]; 235 236 $contentLength = mb_strlen(preg_replace('/&[0-9a-z]{2,8};|&#[0-9]{1,7};|&#x[0-9a-f]{1,6};/i', ' ', $tag[3])); 237 if ($contentLength + $totalLength > $length) { 238 $left = $length - $totalLength; 239 $entitiesLength = 0; 240 if (preg_match_all('/&[0-9a-z]{2,8};|&#[0-9]{1,7};|&#x[0-9a-f]{1,6};/i', $tag[3], $entities, PREG_OFFSET_CAPTURE)) { 241 foreach ($entities[0] as $entity) { 242 if ($entity[1] + 1 - $entitiesLength <= $left) { 243 $left--; 244 $entitiesLength += mb_strlen($entity[0]); 245 } else { 246 break; 231 247 } 232 248 } 233 249 } 234 $truncate .= $tag[1]; 235 236 $contentLength = mb_strlen(preg_replace('/&[0-9a-z]{2,8};|&#[0-9]{1,7};|&#x[0-9a-f]{1,6};/i', ' ', $tag[3])); 237 if ($contentLength + $totalLength > $length) { 238 $left = $length - $totalLength; 239 $entitiesLength = 0; 240 if (preg_match_all('/&[0-9a-z]{2,8};|&#[0-9]{1,7};|&#x[0-9a-f]{1,6};/i', $tag[3], $entities, PREG_OFFSET_CAPTURE)) { 241 foreach ($entities[0] as $entity) { 242 if ($entity[1] + 1 - $entitiesLength <= $left) { 243 $left--; 244 $entitiesLength += mb_strlen($entity[0]); 245 } else { 246 break; 247 } 250 $truncate .= mb_substr($tag[3], 0, $left + $entitiesLength); 251 break; 252 } else { 253 $truncate .= $tag[3]; 254 $totalLength += $contentLength; 255 } 256 if ($totalLength >= $length) { 257 break; 258 } 259 } 260 } else { 261 if (mb_strlen($text) <= $length) { 262 return $text; 263 } else { 264 $truncate = mb_substr($text, 0, $length - mb_strlen($ending)); 265 } 266 } 267 if (!$exact) { 268 $spacepos = mb_strrpos($truncate, ' '); 269 if (isset($spacepos)) { 270 if ($html) { 271 $bits = mb_substr($truncate, $spacepos); 272 preg_match_all('/<\/([a-z]+)>/', $bits, $droppedTags, PREG_SET_ORDER); 273 if (!empty($droppedTags)) { 274 foreach ($droppedTags as $closingTag) { 275 if (!in_array($closingTag[1], $openTags)) { 276 array_unshift($openTags, $closingTag[1]); 248 277 } 249 278 } 250 $truncate .= mb_substr($tag[3], 0, $left + $entitiesLength); 251 break; 252 } else { 253 $truncate .= $tag[3]; 254 $totalLength += $contentLength; 255 } 256 if ($totalLength >= $length) { 257 break; 258 } 259 } 260 } else { 261 if (mb_strlen($text) <= $length) { 262 return $text; 263 } else { 264 $truncate = mb_substr($text, 0, $length - mb_strlen($ending)); 265 } 266 } 267 if (!$exact) { 268 $spacepos = mb_strrpos($truncate, ' '); 269 if (isset($spacepos)) { 270 if ($html) { 271 $bits = mb_substr($truncate, $spacepos); 272 preg_match_all('/<\/([a-z]+)>/', $bits, $droppedTags, PREG_SET_ORDER); 273 if (!empty($droppedTags)) { 274 foreach ($droppedTags as $closingTag) { 275 if (!in_array($closingTag[1], $openTags)) { 276 array_unshift($openTags, $closingTag[1]); 277 } 278 } 279 } 280 } 281 $truncate = mb_substr($truncate, 0, $spacepos); 282 } 283 } 284 $truncate .= $ending; 285 286 if ($html) { 287 foreach ($openTags as $tag) { 288 $truncate .= '</'.$tag.'>'; 289 } 290 } 291 292 return $truncate; 293 } 294 } 279 } 280 } 281 $truncate = mb_substr($truncate, 0, $spacepos); 282 } 283 } 284 $truncate .= $ending; 285 286 if ($html) { 287 foreach ($openTags as $tag) { 288 $truncate .= '</'.$tag.'>'; 289 } 290 } 291 292 return $truncate; 293 } 294 }
Note: See TracChangeset
for help on using the changeset viewer.