Ver Mensaje Individual
  #2 (permalink)  
Antiguo 19/08/2015, 22:07
Avatar de NSD
NSD
Colaborador
 
Fecha de Ingreso: mayo-2012
Ubicación: Somewhere
Mensajes: 1.332
Antigüedad: 12 años
Puntos: 320
Respuesta: Evitar str_ireplace() y usar otra función que solo busque texto

Código PHP:
Ver original
  1. <?php
  2.     function tagHighlight($text, $word, $tag = "strong") {
  3.         $srLen = strlen($word);
  4.         $ohLen = strlen($tag) + 5;
  5.         $iPos = -1;
  6.         while(($iPos = stripos($text, $word, $iPos + 1)) !== false) {
  7.             $text = substr_replace($text, "<$tag>".substr($text, $iPos, $srLen)."</$tag>", $iPos, $srLen);
  8.             $iPos += $srLen + $ohLen;
  9.         }
  10.  
  11.         return $text;
  12.     }
  13.  
  14.     $text = "Hola, Esto es una prueba, y esTo tambien, osea que todo esto, solo sirve para probar a ESTO.";
  15.     $word = "esto";
  16.  
  17.     echo tagHighlight($text, $word);
__________________
Maratón de desafíos PHP Junio - Agosto 2015 en FDW | Reglamento - Desafios