¿Cómo puedo encontrar imágenes vinculadas en PHP?

I want to take some html content and operate on a few particular attributes in a few particular tags.

I am trying to implement the following algorithm in PHP:

  • Find all 'a' tags and for each
    • if its href attribute matches a given url
      • if this tag contains an 'img' tag with a matching 'src' attribute
        • do stuff : may require changing contents/attributes of current 'a' and 'img' tags
      • terminara si
    • terminara si
  • Siguiente página

I am trying to implement this within a wordpress plugin so we can assume that I have all the functionality of that platform available.

What methods/techniques can i use to implement this? i am really bad with regular expressions so i was hoping there might be an easier way to do this or if anyone can recommend a good way to build the regular expressions, that would be acceptable too.

any help would be greatly appreciated!

preguntado el 09 de enero de 11 a las 03:01

2 Respuestas

Whatever you do don't try to parse the HTML using regex!

Parse it with a parser :)

For example DOM.

Respondido el 09 de enero de 11 a las 06:01

DOM classes are the way to go with this. you can load a document, xpath an array of anchor tags, get the href attribute, and everything else you want to do fairly easily. DOM is natively available in PHP. - dqhendricks

any good examples or tutorials on using this specific extension? i haven't done a lot of object oriented PHP before, and i haven't bothered to formally learn the DOM in terms of general XML. - Ankur

There are lots of examples t ofind on the web. For example: phpro.org/examples/Parse-HTML-With-PHP-And-DOM.html. The PHP documentation is also mostly a good place to start. And right here on SO is also a lot of useful info to find. If you still can't figure it out just ask a new question here and we will set you up - PeeHaa

This sounds like a good job for an Analizador de HTML :)

Respondido el 09 de enero de 11 a las 06:01

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.