Ver Mensaje Individual
  #3 (permalink)  
Antiguo 15/08/2010, 21:14
Avatar de destor77
destor77
 
Fecha de Ingreso: noviembre-2004
Ubicación: Gálvez, Santa Fe, Argentina
Mensajes: 2.654
Antigüedad: 19 años, 5 meses
Puntos: 43
Respuesta: cortar texto y tag html

si era algo parecido a eso, pero encontre en el repositorio de cakephp la función parecida a la que estaba usando pero mejorada:
Código PHP:
Ver original
  1. /**
  2.      * corta el texto en x caracteres sin perder el cierre de los tags html
  3.      * @param <string> $text
  4.      * @param <integer> $length
  5.      * @param <array> $options
  6.      * @return <string>
  7.      */
  8.     function cortarTexto($text, $length = 100, $options = array()) {
  9.         $default = array(
  10.             'ending' => '...', 'exact' => true, 'html' => false
  11.         );
  12.         $options = array_merge($default, $options);
  13.         extract($options);
  14.  
  15.         if ($html) {
  16.         if (mb_strlen(preg_replace('/<.*?>/', '', $text)) <= $length) {
  17.             return $text;
  18.         }
  19.         $totalLength = mb_strlen(strip_tags($ending));
  20.         $openTags = array();
  21.         $truncate = '';
  22.  
  23.         preg_match_all('/(<\/?([\w+]+)[^>]*>)?([^<>]*)/', $text, $tags, PREG_SET_ORDER);
  24.         foreach ($tags as $tag) {
  25.             if (!preg_match('/img|br|input|hr|area|base|basefont|col|frame|isindex|link|meta|param/s', $tag[2])) {
  26.             if (preg_match('/<[\w]+[^>]*>/s', $tag[0])) {
  27.                 array_unshift($openTags, $tag[2]);
  28.             } else if (preg_match('/<\/([\w]+)[^>]*>/s', $tag[0], $closeTag)) {
  29.                 $pos = array_search($closeTag[1], $openTags);
  30.                 if ($pos !== false) {
  31.                 array_splice($openTags, $pos, 1);
  32.                 }
  33.             }
  34.             }
  35.             $truncate .= $tag[1];
  36.  
  37.             $contentLength = mb_strlen(preg_replace('/&[0-9a-z]{2,8};|&#[0-9]{1,7};|&#x[0-9a-f]{1,6};/i', ' ', $tag[3]));
  38.             if ($contentLength + $totalLength > $length) {
  39.             $left = $length - $totalLength;
  40.             $entitiesLength = 0;
  41.             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)) {
  42.                 foreach ($entities[0] as $entity) {
  43.                 if ($entity[1] + 1 - $entitiesLength <= $left) {
  44.                     $left--;
  45.                     $entitiesLength += mb_strlen($entity[0]);
  46.                 } else {
  47.                     break;
  48.                 }
  49.                 }
  50.             }
  51.  
  52.             $truncate .= mb_substr($tag[3], 0 , $left + $entitiesLength);
  53.             break;
  54.             } else {
  55.             $truncate .= $tag[3];
  56.             $totalLength += $contentLength;
  57.             }
  58.             if ($totalLength >= $length) {
  59.             break;
  60.             }
  61.         }
  62.         } else {
  63.         if (mb_strlen($text) <= $length) {
  64.             return $text;
  65.         } else {
  66.             $truncate = mb_substr($text, 0, $length - mb_strlen($ending));
  67.         }
  68.         }
  69.         if (!$exact) {
  70.         $spacepos = mb_strrpos($truncate, ' ');
  71.         if (isset($spacepos)) {
  72.             if ($html) {
  73.             $bits = mb_substr($truncate, $spacepos);
  74.             preg_match_all('/<\/([a-z]+)>/', $bits, $droppedTags, PREG_SET_ORDER);
  75.             if (!empty($droppedTags)) {
  76.                 foreach ($droppedTags as $closingTag) {
  77.                 if (!in_array($closingTag[1], $openTags)) {
  78.                     array_unshift($openTags, $closingTag[1]);
  79.                 }
  80.                 }
  81.             }
  82.             }
  83.             $truncate = mb_substr($truncate, 0, $spacepos);
  84.         }
  85.         }
  86.         $truncate .= $ending;
  87.  
  88.         if ($html) {
  89.         foreach ($openTags as $tag) {
  90.             $truncate .= '</'.$tag.'>';
  91.         }
  92.         }
  93.  
  94.         return $truncate;
  95.  
  96.  
  97.     }

salu2