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

Cita:
Iniciado por pateketrueke Ver Mensaje


Código PHP:
<?php
      $html
='<html><body><a class="footer" href="#">Área privada.</a><span class="hola" asfda sfasdf/><a class="aas" href="#"><span>Área privada.</span></a></span></body></html>';
      
preg_match_all('/<(\w+)[^>]*\/?>/',$html,$matches);
      echo 
"<pre>";
      echo 
htmlspecialchars(print_r($matches,1));
      echo 
"</pre>";
?>
¿que tal así?? ;)
Mucha gracias pateketrueke,

He probado tu código y gracias a él pude llegar casi hasta donde quería, solo falta poderle decir que tags quieres excluir, dentro del a RegExp.

Código PHP:
Ver original
  1. function Extraer_Tags_HTML($html){
  2.     preg_match_all('/<(\w+)[^>]*\/?>/',$html,$matches);
  3.     echo "<pre>";
  4.     echo print_r($matches[1],1);
  5.     echo "</pre>";
  6.     $sin_duplicados=array_unique($matches[1]); //elimina los tags HTML repetidos
  7.    
  8.     return $sin_duplicados;
  9. }
  10.  
  11. // Imprime el array de tags HTML
  12. $resultado=Extraer_Tags_HTML($html);
  13. foreach($resultado as $value)
  14.     echo $value . "<br/>";

Salida correcta

Código:
Array
(
    [0] => html
    [1] => head
    [2] => title
    [3] => meta
    [4] => meta
    [5] => link
    [6] => link
    [7] => body
    [8] => p
    [9] => a
    [10] => div
    [11] => h1
    [12] => form
    [13] => p
    [14] => label
    [15] => br
    [16] => input
    [17] => p
    [18] => label
    [19] => br
    [20] => input
    [21] => p
    [22] => label
    [23] => input
    [24] => p
    [25] => input
    [26] => p
    [27] => a
)

html
head
title
meta
link
body
p
a
div
h1
form
label
br
input
Si quiero poder introducir los tags html que quiero que no me saque ej. meta y title no se haría con la negación ^[^meta|title] ?

Saca todo igual que antes, menos todos los tags que empiecen por META o TITLE.

preg_match_all('/<^[^meta|title](\w+)[^>]*\/?>/',$html,$matches);

No entiendo porque es tan difícil!!!!!!!!!