Foros del Web » Programando para Internet » PHP » Frameworks y PHP orientado a objetos »

Clase para obtener los metas y solo texto de una web

Estas en el tema de Clase para obtener los metas y solo texto de una web en el foro de Frameworks y PHP orientado a objetos en Foros del Web. Hola, hice la siguiente clase para poder obtener los metas, el titulo y el texto sin tags html de una web. Espero que les sirva! ...
  #1 (permalink)  
Antiguo 28/08/2008, 08:46
 
Fecha de Ingreso: noviembre-2007
Mensajes: 38
Antigüedad: 16 años, 4 meses
Puntos: 2
Clase para obtener los metas y solo texto de una web

Hola, hice la siguiente clase para poder obtener los metas, el titulo y el texto sin tags html de una web.

Espero que les sirva!

Mas explicaciones en mi blog;
http://eugeniofage.wordpress.com/2008/08/28/clase-spider-para-obtener-metas-y-contenido-sin-html-tags-de-una-web/

Código PHP:
//@author Eugenio Fage
abstract class Spider {
    public function 
getWebFull($url){
        
$htmlCode=self::getWebCode($url);
        if(
$htmlCode=='') return array();
        
$return['title']=self::getTitle($htmlCode);
        
$return['metas']=self::getMetas($htmlCode);

        
$return['text']=self::justText($htmlCode);

        return 
$return;
    }

    public function 
getWebCode($url){
        
//@todo si no existen las curl functions usar fsockopen
        
$ch curl_init();

        
curl_setopt ($chCURLOPT_URL$url);
        
curl_setopt($chCURLOPT_FOLLOWLOCATIONtrue);
        
curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
        
curl_setopt($chCURLOPT_USERAGENT"Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
        
$data curl_exec ($ch);
        
curl_close ($ch);

        return 
$data;
    }

    public function 
getTitle($html,$charset=null){
        
//@todo corregir acentos sin usar multi byte functions
        
$arr=array();
        
preg_match_all('@(<title>(.*)</title>)@i',$html,$arr);
        
$arr=$arr[2];
        
//el titulo no va a ser mas largoque 100 caracteres
        
return(substr(strip_tags($arr[0]),0,110));
    }

    public function 
getMetas($html,$charset=null){
        
//@todo corregir acentos sin usar multi byte functions
        
$arr=array();
        
preg_match_all('@(meta\sname=\"(.*)\"\scontent=\"(.*)\"[ /]*>)@i',$html,$arr);
        
$meta=$arr[2];
        
$content=$arr[3];
        unset(
$arr);

        while((
$unMeta=array_pop($meta))){
            
$metas[strtolower($unMeta)]=array_pop($content);
        }

        while((
$unMeta=array_pop($meta))){
            
$metas[strtolower($unMeta)]=array_pop($content);
        }

        
preg_match_all('@(meta\scontent=\"(.*)\"\sname=\"(.*)\"[ /]*>)@i',$html,$arr);
        
$meta=$arr[3];
        
$content=$arr[2];
        unset(
$arr);

        while((
$unMeta=array_pop($meta))){
            
$metas[strtolower($unMeta)]=array_pop($content);
        }

        return 
$metas;
    }

    public function 
justText($html,$charset=null){
        
//@todo corregir acentos sin usar multi byte functions
        
$html=str_replace('>','> '$html);

        
$buscar=array('@<!--.*?-->@si','@<script[^>]*?>.*?</script>@si','@<style[^>]*?>.*?</style>@si');
        
$html preg_replace($buscar' '$html);

        
$html preg_replace('@<.*?>@si'' '$html);

        
$html=str_replace('&lt;',' ',$html);
        
$html=str_replace('&gt;',' ',$html);

        
$html=html_entity_decode(strip_tags($html));

        
$html=str_replace(array('<','>','&gt;','&lt;',"\t",chr(13),chr(10),chr(160)),' ',$html);

        while(
strpos($html,'  ')!==false){
            
$html=str_replace('  ',' ',$html);
        }

        return 
substr($html,0,1500);
    }



Última edición por eugenioclrc; 28/08/2008 a las 11:01 Razón: agregar el codigo
  #2 (permalink)  
Antiguo 28/08/2008, 10:54
Avatar de GatorV
$this->role('moderador');
 
Fecha de Ingreso: mayo-2006
Ubicación: /home/ams/
Mensajes: 38.567
Antigüedad: 17 años, 11 meses
Puntos: 2135
Respuesta: Clase para obtener los metas y solo texto de una web

Deberías de copiar y pegar tu clase aquí sí lo que deseas es compartirla.

Saludos.
  #3 (permalink)  
Antiguo 03/09/2008, 13:18
hen
 
Fecha de Ingreso: diciembre-2003
Ubicación: Buenos Aires
Mensajes: 63
Antigüedad: 20 años, 4 meses
Puntos: 1
Respuesta: Clase para obtener los metas y solo texto de una web

Muy bueno :)

Cita:
Iniciado por eugenioclrc Ver Mensaje
Hola, hice la siguiente clase para poder obtener los metas, el titulo y el texto sin tags html de una web.

Espero que les sirva!

Mas explicaciones en mi blog;
http://eugeniofage.wordpress.com/200...gs-de-una-web/

Código PHP:
//@author Eugenio Fage
abstract class Spider {
    public function 
getWebFull($url){
        
$htmlCode=self::getWebCode($url);
        if(
$htmlCode=='') return array();
        
$return['title']=self::getTitle($htmlCode);
        
$return['metas']=self::getMetas($htmlCode);

        
$return['text']=self::justText($htmlCode);

        return 
$return;
    }

    public function 
getWebCode($url){
        
//@todo si no existen las curl functions usar fsockopen
        
$ch curl_init();

        
curl_setopt ($chCURLOPT_URL$url);
        
curl_setopt($chCURLOPT_FOLLOWLOCATIONtrue);
        
curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
        
curl_setopt($chCURLOPT_USERAGENT"Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
        
$data curl_exec ($ch);
        
curl_close ($ch);

        return 
$data;
    }

    public function 
getTitle($html,$charset=null){
        
//@todo corregir acentos sin usar multi byte functions
        
$arr=array();
        
preg_match_all('@(<title>(.*)</title>)@i',$html,$arr);
        
$arr=$arr[2];
        
//el titulo no va a ser mas largoque 100 caracteres
        
return(substr(strip_tags($arr[0]),0,110));
    }

    public function 
getMetas($html,$charset=null){
        
//@todo corregir acentos sin usar multi byte functions
        
$arr=array();
        
preg_match_all('@(meta\sname=\"(.*)\"\scontent=\"(.*)\"[ /]*>)@i',$html,$arr);
        
$meta=$arr[2];
        
$content=$arr[3];
        unset(
$arr);

        while((
$unMeta=array_pop($meta))){
            
$metas[strtolower($unMeta)]=array_pop($content);
        }

        while((
$unMeta=array_pop($meta))){
            
$metas[strtolower($unMeta)]=array_pop($content);
        }

        
preg_match_all('@(meta\scontent=\"(.*)\"\sname=\"(.*)\"[ /]*>)@i',$html,$arr);
        
$meta=$arr[3];
        
$content=$arr[2];
        unset(
$arr);

        while((
$unMeta=array_pop($meta))){
            
$metas[strtolower($unMeta)]=array_pop($content);
        }

        return 
$metas;
    }

    public function 
justText($html,$charset=null){
        
//@todo corregir acentos sin usar multi byte functions
        
$html=str_replace('>','> '$html);

        
$buscar=array('@<!--.*?-->@si','@<script[^>]*?>.*?</script>@si','@<style[^>]*?>.*?</style>@si');
        
$html preg_replace($buscar' '$html);

        
$html preg_replace('@<.*?>@si'' '$html);

        
$html=str_replace('&lt;',' ',$html);
        
$html=str_replace('&gt;',' ',$html);

        
$html=html_entity_decode(strip_tags($html));

        
$html=str_replace(array('<','>','&gt;','&lt;',"\t",chr(13),chr(10),chr(160)),' ',$html);

        while(
strpos($html,'  ')!==false){
            
$html=str_replace('  ',' ',$html);
        }

        return 
substr($html,0,1500);
    }


__________________
.:hEN
DevHen
EXITOS
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 05:35.