Written by davidonzo on 11/10/2008, Filled in OpenSource, Tutorial
WARNING!
This article has been written 692 days ago.
The informations may be out of dated!

php at davidonzo.comI was working on some template modification of this blog, when I dediced to make a php image extractor function. I don't love to use big class with thousend of code lines to parse html.

 

Looking on PHP5 core it seems to be some DOM function available really usefull to parse html document easly. Using DOMXPath class and DOMElement::getAttribute function you'll need just some beet line of code to extract the needed html string.

 

The easiest way to explain it, is write the code :-)

 

<?php
  $myVar = "Ciao <br /><img src=\"11.jpg\" alt=\"foto\" /><br /><img src=\"12.jpg\" alt=\"foto\" />";
 
  $doc = new DOMDocument;
  $doc->loadHTML($myVar);
 
  $xpath = new DOMXPath($doc);
 
  $nodo = $xpath->query('//img[@src]');
    foreach ($nodo as $nodo){
    $imgsrc = $nodo->getAttribute('src');
    echo "<img src=\"".$imgsrc."\" alt=\"myMagicImage\" />";
  }
?>

 

Short and sweety, isn't it? :-)

Did you find interesting this article?
Subscribe my feed to be advised of any new post!
 
.Comments rss
# 1
Grande!
Proprio ieri mi scervellavo per trovare un modo semplice per impaginare le immagine estratte dai feed rss, e questo sistema mi sembra ottimo, con le dovute modifiche :-)

Anche noi ingorantoni di programmazione possiamo andare avanti con soluzioni prese da altri. Forse è questo che significa ion realtà "web 2-0" :-D
By Leandro  (Sent 14/10/2008 @ 13:41:28)
# 2
Felice di essere utile :)

Ma ricorda di usare $doc->loadXML se è un feed.
Poi con $doc->getElementsByTagName per prendere l'elemento dell'albero XML che ti interessa.
By davidonzo  (Sent 14/10/2008 @ 16:50:39)
# 3
Salve, non sapendo come contattarla lascio un commento qui..
Volevo sapere se era interessato ad effettuare uno scambio link.
Il mio sito è il seguente
www.pcweblog.it
By lillobyte91  (Sent 14/10/2008 @ 19:37:45)
# 4
Ciao davidonzo, da premettere che non sono una cima nella programmazione... Vado subito al punto:
Cercavo un modo per estrarre da una serie di post soltanto il tag <object>, in modo da creare, in un'altra pagina, una galleria dei video (youtube li mette dentro quel tag lì).
Ho preso il tag "object" come esempio.. potrebbe anche essere <img> o <p>.
Devo assegnare ad ogni tag un ID, in modo da renderlo riconoscibile dallo script php? (suona male anche a me)
Devo usare il link del post singolo come punto di riferimento?

Non so cosa fare, help mee :((
Grazie e complimenti per il blog, molto utile!!!
By Milk-Break  (Sent 16/12/2008 @ 03:03:42)
# 5
@ Milk: una volta che prendi tutti i tag object hai un array.

Se ci sono tre filmati nella pagina, avrai un array con i tre <object bla bla bla ></object>.

Li prendi e con un semplice ciclo foreach ne fai quello che vuoi.
Basta saper gestire un array.
By davidonzo  (Sent 16/12/2008 @ 12:59:56)


Comments could be moderated.
If you don't see it immediately published, please avoid to insert it one more time.
Thanks for your patient.