function truncatePreserveWords($text, $needles, $w_near_keywords = 30, $class = "highlight") { $b = explode(" ", trim(strip_tags($text))); //haystack words $c = array(); //array of words to keep/remove for ($j = 0; $j < count($b); $j++) { $c[$j] = false; } for ($i = 0; $i < count($b); $i++) { for ($k = 0; $k < count($needles); $k++) { if (stristr($b[$i], $needles[$k])) { //$b[$i]=preg_replace("/".$needles[$k]."/i","<$tag $class>\\0</$tag>",$b[$i]); for ($j = max($i - $w_near_keywords, 0); $j < min($i + $w_near_keywords, count($b)); $j++) { $c[$j] = true; } } } } $o = ""; // reassembly words to keep for ($j = 0; $j < count($b); $j++) { if ($c[$j]) { $o .= " " . $b[$j]; } else { $o .= "."; } } return highlight_phrases(preg_replace("/\\.{3,}/i", "...", $o), $needles, $class); }
<li> <h2 style="font-size: 1.3em;"> <?php if ($ficha->Maestro->sello_chilesinpapeleo) { ?> <img class="sello-chilesinpapeleo has-tooltip-chilesinpapeleo" title="Este sello es otorgado a los trámites del Estado que se realizan completamente por Internet y no requieren presencia física de las personas para su realización." src="<?php echo base_url('assets/images/ico_chilesinpapeleo_32_on.png'); ?> " alt="Sello ChileSinPapeleo"> <?php } ?> <a href="<?php echo "fichas/ver/" . $ficha->maestro_id; ?> " ><? echo (is_array($needles) && count($needles) > 0) ? highlight_phrases($ficha->titulo, $needles) : $ficha->titulo; ?></a> </h2> <p> <?php echo introObjetivoFicha($ficha, $needles, $this->texthighlight); ?> <a href="<?php echo "fichas/ver/" . $ficha->maestro_id; ?> " >Ver más</a> </p> <?php if ($ficha->guia_online_url && $ficha->Maestro->sello_chilesinpapeleo) { ?> <div class="t_online"> <a href="<?php