Ver Mensaje Individual
  #26 (permalink)  
Antiguo 19/01/2010, 12:58
Avatar de neodani
neodani
 
Fecha de Ingreso: marzo-2007
Mensajes: 1.811
Antigüedad: 17 años, 2 meses
Puntos: 20
Respuesta: Encontrar etiquetas html, class y id's (con expresiones regulares)

Continuo el POST anterior con el script y los errores que intento solucionar

super_parser.php

Código PHP:
Ver original
  1. <?
  2. /*
  3. Objetivo: Extraer todos los atributos HTML, y propiedades CSS (id y class)
  4. Para compararlos con una hoja de estilo y eliminar las propiedades que no se usen.
  5.  
  6. Archivo web.htm >> extraemos los atributos HTML, ID y CLASS
  7. Archivo style.css >> comparamos los atributos extraidos en el archivo web.htm
  8.                       y nos quedamos con la parte útil del fichero style.css
  9. */
  10.  
  11. function Extraer_Tags_HTML($file_html){
  12.     $html = file_get_contents($file_html);
  13.     // En el primer parantesis le decimos los tags que descarta
  14.     preg_match_all('/<(?!meta|title|head|link)(\w+)[^>]*\/?>/',$html,$matches);
  15.     $sin_duplicados=array_unique($matches[1]); //elimina los tags HTML repetidos
  16.    
  17.     return $sin_duplicados;
  18. }
  19.  
  20. function Extraer_Class_ID($file_html){
  21.     $html = file_get_contents($file_html);
  22.     $clases = array();
  23.     $id = array();
  24.    
  25.     preg_match_all('/<(a|p|form|h1|label|input|div|body)[^>](?:(class)=["|\'](.*?)["|\'])|(?:(id)=["|\'](.*?)["|\'])[^>]*>/is',$html,$matches);
  26.  
  27.     //eliminamos los repetidos
  28.     $clases=array_unique($matches[3]);
  29.     $id=array_unique($matches[5]);
  30.    
  31.     //Quedara un valor en cada array en blanco, lo eliminamos así
  32.     foreach($clases as $item){  if ($item!=''){$resultado_class[] = $item;} }
  33.     foreach($id as $item){  if ($item!=''){$resultado_id[] = $item;} }
  34.    
  35.     return array($resultado_id,$resultado_class);
  36. }
  37.  
  38. function parser_css ($file_css,$patron){
  39.     $css = file_get_contents($file_css);
  40.     preg_match_all("/[#\s\w:-]*$patron\s*[#\s\w:-]*\{[^\}]*\}/is",$css,$matches);
  41.    
  42.     return $matches[0];
  43. }
  44.  
  45. // -------- PROGRAMA -----------------
  46. $file_html='web.htm';
  47. $file_css='style.css';
  48. $css = file_get_contents($file_css);
  49.  
  50. // Imprime el array de tags HTML
  51. $tags_html=Extraer_Tags_HTML($file_html);
  52. $estilo=Extraer_Class_ID($file_html);
  53.  
  54. echo "<pre>";
  55. echo "HTML<br/>";
  56. print_r($tags_html);
  57. echo "ID<br/>";
  58. print_r($estilo[0]);
  59. echo "<br/>CLASS<br/>";
  60. print_r($estilo[1]);
  61. echo "</pre>";
  62.  
  63. // ---->> TAGS HTML
  64. echo "<br/><br/> <strong>TAGS HTML</strong> <br/><br/>";
  65. foreach($tags_html as $value){
  66.     preg_match_all("/[.\#\s\w:-]*$value\s*[#\s\w:-]*\{[^\}]*\}/is",$css,$matches);
  67.     foreach ($matches[0] as $propiedad){
  68.         echo $propiedad;
  69.     }
  70. }
  71. // ---->> TAGS ID
  72. echo "<br/><br/> <strong>TAGS ID</strong> <br/><br/>";
  73. foreach($estilo[0] as $value){
  74.     echo $value;
  75.     preg_match_all("/[\.#\s\w:-]*#\$value\s*[#\s\w:-]*\{[^\}]*\}/is",$css,$matches);
  76.     foreach ($matches[0] as $propiedad){
  77.         echo $propiedad;
  78.     }
  79. }
  80.  
  81. // ---->> TAGS CLASS
  82. echo "<br/><br/> <strong>TAGS CLASS</strong> <br/><br/>";
  83. foreach($estilo[1] as $value){
  84.     echo $value;
  85.     preg_match_all("/[\.#\s\w:-]*\.\$value\s*[#\s\w:-]*\{[^\}]*\}/is",$css,$matches);
  86.     foreach ($matches[0] as $propiedad){
  87.         echo $propiedad;
  88.     }
  89. }
  90.  
  91. ?>


La expresión regular de los tags ID

Código PHP:
Ver original
  1. preg_match_all("/[\.#\s\w:-]*#\$value\s*[#\s\w:-]*\{[^\}]*\}/is",$css,$matches);

Y la de los CLASS no funcionan como debería

Código PHP:
Ver original
  1. preg_match_all("/[\.#\s\w:-]*\.\$value\s*[#\s\w:-]*\{[^\}]*\}/is",$css,$matches);

Podéis echarme una mano, estoy seguro que es algo muy pequeño que se me está escapando y no lo veo

Muchas gracias de antemano!