How to create Simple_HTML_DOM output

I get the images and their URLs with the following code using Simple HTML DOM Parser:

<?php
    include_once('simple_html_dom.php');
    $url = "http://www.tokyobit.com";
    $html = new simple_html_dom();
    $html->load_file($url);
    foreach($html->find('img') as $img){
        echo $img . "<br/>";
        echo $img->src . "<br/>";
    }
?>

But the result does not look so good:

output
(source: netdna-cdn.com )

So, how can I style the output in CSS as with adding a class to each image and its src. My CSS:

.image-and-src {
    border: 2px solid #777;
} 

So how can I add this class ?: image-and-src

+4
source share
3 answers
foreach($html->find('img') as $img){
    echo '<div class="img-and-src">';
    echo $img . "<br/>";
    echo $img->src . "<br/>";
    echo '</div>';
}

Two lines added to the code wrap the contents of echo'd in a div with your class while it loops. Now you have the opportunity to also wrap the text in between, styling them separately.

, @Ajeet Manral:)

+2

foreach($html->find('img') as $img){

        echo $img->src . "<br/>";
        echo '<img src="'.$img->src.'" width=100% height=100px><br/>';
    }
0

template file

<!DOCTYPE html>
<html>
<head>
  <title>Your title</title>
</head>
<body>
  <h1>Somebody images</h1>
  <?php foreach($html->find('img') as $img) { ?>
    <!-- put some pretty looking html here  -->
  <?php } ?>
<body>
</html>

if you don’t know about templates, I suggest some research on

0
source

All Articles