Ver Mensaje Individual
  #1 (permalink)  
Antiguo 09/07/2009, 09:36
Avatar de Italico76
Italico76
 
Fecha de Ingreso: abril-2007
Mensajes: 3.303
Antigüedad: 17 años
Puntos: 292
Clase de parseo en links, imagenes, videos y mas [APORTE]

Bueno...aca una clase para parsear:

- TITLE de la pagina
- META TAGS
- ENLACES
- IMAGENES
- VIDEOS *** new ***

Código PHP:
<?php
//// By Pablo Bozzolo (2009)
///
///  Expresion regular para links by Chirp Internet: www.chirp.com.au


Include "links.class.php";
Include 
"video.class.php";

class 
tagParser {  // clase 
//////////////////////////////////////
  
private   $html
  
  protected 
$title// title de la pagina
  
protected $titleProcesado=FALSE;       #bool
  
  
protected $metatags;
  protected 
$metatagsProcesados=FALSE;   #bool
  
  
protected $enlaces;
  private   
$cantEnlaces;
  private   
$contadorEnlaces;
  protected 
$enlacesProcesados=FALSE;    #bool
  
  
protected $imagenes;  
  private   
$cantImagenes;    
  private   
$contadorImagenes;  
  protected 
$imagenesProcesadas=FALSE;   #bool
  
  
protected $videos;
  private   
$cantVideos;
  private   
$contadorVideos;
  protected 
$videosProcesados=FALSE;     #bool
  //////////////////////////////////////  
  
  
function __construct($html){ 
   
$this->html=$html;    
   
$this->html =str_replace("\"" "'" ,$this->html);  //reemplazo comillas dobles por simples para simplificar expr reg   
  

  
  
//// LINKS
  
  
function parseLinks(){      
    
preg_match_all("/<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>(.*)<\/a>/siU",$this->html,$this->enlaces);  // expresion para parseo de LINKS    
    
$this->cantEnlaces=count($this->enlaces[2]);
    
$this->contadorEnlaces=0// procesados
    
$this->enlacesProcesados=TRUE;

  }    

  function 
getLinkCount(){
    if (
$this->enlacesProcesados){  
      return(
$this->cantEnlaces);
    }else{
      echo 
"ERROR: NO HA PROCESADO NINGUN ENLACE!";
      exit;     
   }      
  }
  
  function 
getLink(){       
    
$uri str_replace("'","",$this->enlaces[2][$this->contadorEnlaces]);     
    
$diana $this->enlaces[3][$this->contadorEnlaces];    
    
$this->contadorEnlaces++;
    
$linktogive = new link ($uri);
    
$linktogive->setDiana($diana);
    return (
$linktogive);   
  }  
  
  
//// Imagenes
  
  
function parseImages(){  
    
preg_match_all("|<img\s[^>]*src=(\"??)([^\" >]*?)\\1[^>]*/>|siU",$this->html,$this->imagenes);  // expresion para parseo de LINKS    
    
$this->cantImagenes=count($this->imagenes[1]); // BUG fixed 
    
$this->contadorImagenes=0// procesados
    
$this->imagenesProcesadas=TRUE;    
  }
  
  function 
getImageCount(){
    if (
$this->imagenesProcesadas){  
      return(
$this->cantImagenes);
    }else{
      echo 
"ERROR: NO HA PROCESADO NINGUNA IMAGEN!";
      exit;     
   }      
  }
    
  function 
getImage(){    
    
$img     str_replace("'","",$this->imagenes[2][$this->contadorImagenes]); 
    
$imgtogive = new Img($img); 
    
$this->contadorImagenes++;
    return (
$imgtogive);   
  }  

  
//// TITLE de la pagina
  
  
function parseTitle(){
    
preg_match('|<title>(.*?)</title>|is',$this->html,$title);      
    
$this->title=$title[1];
    
$this->titleProcesado=TRUE;
  }
  
  function 
getTitle(){
    if (
$this->titleProcesado){  
      return (
$this->title);
    }else{  
      echo 
"ERROR: NO HA PROCESADO NINGUN TITLE";
      exit;     
    } 
  }
  
  
//// META-tags 
  
  
function parseMetas(){
    
save_file("parser.tmp",$this->html); // generar uno q no exista        
    
$this->metatags get_meta_tags("parser.tmp");
    
$this->titleProcesado=TRUE;
  }    
  
  function 
getMeta($tag){
    return (
$this->metatags[$tag]);
  }
  
  
//// VIDEOS
  
  
function parseVideos(){
  
    
// YouTube    
    
preg_match_all'/value=\'(http:\/\/[^&"\'\s]+)/i',$this->html,$this->videos);        
        
    
// MeyaCafe
    
preg_match_all('|http://www.metacafe.com/fplayer/(.*?).swf|',$this->html,$metacafe);    
    
    for(
$i=0;$i<count($metacafe[1]);$i++){       // lo paso al mismo array
      
$this->videos[1][]="http://www.metacafe.com/fplayer/{$metacafe[1][$i]}.swf";
     }
    

    
$this->cantVideos=count($this->videos[1]);     
    
$this->contadorVideos=0
    
$this->videosProcesados=TRUE;                
  }
  
   function 
getVideoCount(){
    if (
$this->videosProcesados){  
      return(
$this->cantVideos);
    }else{
      echo 
"ERROR: NO HA PROCESADO NINGUN VIDEO";
      exit;     
   }      
  }  
  
  function 
getVideo(){ // podria recibir el tipo como parametro (YouTube,MetaCafe,etc)    

    
$videotoget = new video($this->videos[1][$this->contadorVideos]);
    
$this->contadorVideos++;
    return (
$videotoget); 
  }
    

}
# fin de la clase


  
Function save_file ($file,$content){  
  
//guardo $content en un archivo
    
if (!$handle fopen($file,"w+b")){
      echo 
"Error: archivo no pudo ser abierto";
      return (
False);
      exit;
    }  
    if (!
fwrite ($handle$content)){
      echo 
"Se ha producido un error y nada se ha escrito";
      return (
False);
      exit;
   }  
   return (
true);  // todo ok
  
}
    

?>
Para probarlo deben bajarse las otras clases que puse mas abajo y aca un ejemplo de uso:

Código PHP:
<?
Include "tag_parser.class.php"// la clase principal

$contenido='<body><title>Pagina de Pablo</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/><meta name="keywords" content="Metatags,Buscador,HTML,Página web" />
</body>Hola <a    href="http://www.mundo.es"  title="cualquier fruta" rel=\'nofollow\'    ><img src="pepe.jpg"/></a>!, loco que <a href="http://www.cruel.com">cruel</a> que sos por <a href="dios.net">Dios</a> ja <p/>

<object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/N9afSH6I3a0&hl=en&fs=1&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/N9afSH6I3a0&hl=en&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object>

<p><br /><div align="center"><embed pluginspage="http://www.macromedia.com/go/getflashplayer" src="http://www.metacafe.com/fplayer/223037/technology_cooking.swf" width="400" height="345" type="application/x-shockwave-flash" flashvars="altServerURL=http://www.metacafe.com&amp;playerVars=videoTitle=Technology CookingshowStats=yesautoPlay=noblogName=mas tecnoblogURL=http://mastecno.com" wmode="transparent"></embed> </div>

<img style="cursor: pointer;" src="http://1.bp.blogspot.com/_CP8nAQLnf5Y/RpvbtUlyH-I/AAAAAAAAArI/Au7GZ82_I_U/s320/candado2.jpg" alt="" id="BLOGGER_PHOTO_ID_5087901775712886754" border="0" /><!-- no lo parsea! por el style delante -->
<a id="202" title="google uk" href="http://www.google.uk" style=\'color:red\'>Google UK</a>
<a href="pepe.com" >Pepe</a><p/> <IMG   SRC="casa.jpg"   /> '
;

$tags  = new tagParser($contenido);

$tags->parseLinks();

for (
$i=0;$i<$tags->getLinkCount();$i++){
  
$enlace $tags->getLink();
  
$uri $enlace->getUrl();
  
$anchor $enlace->getDiana();
  echo 
"$uri<br/>$anchor<p/>";
}

$tags->parseImages();

for (
$i=0;$i<$tags->getImageCount();$i++){
  
$img $tags->getImage()->getImg();  
  echo 
"$img<br/>";
}

$tags->parseTitle();
echo 
$tags->getTitle();

$tags->parseMetas();
echo 
$tags->getMeta('keywords');

echo 
'<p/>';

$tags->parseVideos();

for (
$i=0;$i<$tags->getVideoCount();$i++){        
  
$video    $tags->getVideo()->getSource();     
  echo 
"$video<br/>";
}  

?>

Espero la encuentren util
__________________
Salu2!

Última edición por Italico76; 13/07/2009 a las 13:25