Ver Mensaje Individual
  #8 (permalink)  
Antiguo 27/09/2016, 18:20
rafaelg21
 
Fecha de Ingreso: junio-2008
Mensajes: 74
Antigüedad: 15 años, 11 meses
Puntos: 2
Respuesta: Extraer texto de una oracion dada una palabra a buscar

Bueno amigo he desarrollado una solucion al problema espero con mas tiempo poder optimizarlo un poco mas. pero alli va bien y es funcional por lo que continuare con mi proyecto y ya luego lo optimizare. o si alguno se anima a simplificarlo mas seria de utilidad. una vez mas muchas gracias por sus respuestas...

<?php
function getSubContent($string, $pos, $length=NULL)
{

$lng=strlen($string);
if ($length == NULL)
$length = 50;
if($pos>$length) {
$inicio=$pos-$length;
$hasta=$length;
}else{
$inicio=0;
$hasta=$pos;
}
if($pos>0){
$stringDisplay = substr(strip_tags($string), $inicio,$hasta);
}
if($inicio!=0){
$stringDisplay = '...'.$stringDisplay;
}
$stringDisplay .= substr(strip_tags($string),$pos, $length);

if (strlen(strip_tags($string)) > $length)
$stringDisplay .= '...';
return $stringDisplay;
}

$str="In a metus vel nulla tincidunt bibendum. Ut lorem ipsum, maximus at mattis sit amet, maximus non nunc. Fusce mattis enim est, et iaculis nibh convallis vel. Quisque cursus, purus non elementum tincidunt, dui nulla vulputate mi, ac tincidunt magna purus eu magna. Nullam euismod nibh et neque lobortis";

$q="lorem"; //texto a buscar

$m=strpos(strtolower($str),strtolower($q)); //POS DE LA PALABRA

//SIN DARLE ESTILO
print getSubContent(q,$m,100);

//Y PARA RESALTAR MI PABRA LE COLOQUE UN ESPAN CON ESTILO CSS
print str_ireplace($q,"<span class='destacar'>".strtolower($q).'</span>',getSubContent($str,$m,100));

?>