简体   繁体   中英

simple html dom get 2 tags for one foreach

How to call 2 tags for one foreach with simple html dom?

<?php
require_once("simple_html_dom.php");
$str='<img src="./1.jpg" /><span>image1</span><img src="./2.jpg" /><span>image2</span>';//still have more 'img' and 'span'
$html = str_get_html($str);
foreach($html->find('img') as $content){
    echo $content.'<br />';
    //echo <span> inner html
}
?>

I want to get the result like:

<img src="./1.jpg" />
image1

<img src="./2.jpg" />
image2

one img and behind span for one unit. Thanks.

You could do this.

$img = $html->find("img");
$span = $html->find("span");

for($i=0;$i<count($img);$i++) {
  echo $img[$i] . "<br />" . $span[$i]; 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM