How do i get multiple images?

How do I get multiple images from a single string

  • I've looked around and haven't found a answer that did it for me. I don't want all the images from an HTML page. I just want all the images from a single string. On a page, I'm using two different strings. I want all the images that are within the second image. I want to loop them into a carousel. I've looked around a bit and this is what I got: function GetImgString($plaatje){ preg_match_all('/<img[^>]+>/i',$plaatje, $result); $house = $result[0]; $i = 1; $output = '<div class="carousel-inner">'; foreach ( $house as $houses ) { if ($i == 1) { $output.= '<div class="item active">'; }else{ $output.= '<div class="item">'; } $output.= $house.' <div class="container"> <div class="carousel-caption"> </div> </div> </div> '; $i++; } $output .= '</div>'; return $house;} This is the output: Array ( [0] => &ltimg src="images/Huizen/huis-3.jpg" alt="" /> [1] => &ltimg src="images/Huizen/huis-4.jpg" alt="" /> ) How do I solve this?

  • Answer:

    I assume, when you do the regexp, in your results, there are the 2 images. You are start to building your output into a variable, calles $output, but you return with $house, what contain the 2 result from preg_match. So you can try with this: function GetImgString($plaatje) { preg_match_all('/<img[^>]+>/i', $plaatje, $result); $houses = $result[0]; $houses = str_ireplace('<', '&lt', $house); $houses = str_ireplace('>', '&gt', $house); $i = 1; $output = '<div class="carousel-inner">'; foreach ($houses as $house) { $class = ' class="item"'; if ($i === 1) { $class = ' class="item active"'; } $output.= '<div '.$class.'>'; $output.= $house . ' <div class="container"> <div class="carousel-caption"></div> </div> </div>' . "\n"; $i++; } $output .= '</div>'; return $output; }

ToluT at Stack Overflow Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.