3v4l.org

run code in 300+ PHP versions simultaneously
<?php $content = <<<EOT <p>List of sample images.</p> <img src="https://placehold.it/250x100/99cc00/000.jpg?text=JPG" alt="JPG" /><br> <img src="https://placehold.it/250x100.gif?text=GIF" alt="GIF" /><br> <img src="https://placehold.it/250x100/ff6600/000.png?text=PNG" alt="PNG" /><br> <img class="no-ext" src="https://placehold.it/350x150?text=No Extension" alt="No Ext" /><br> <img src="https://placehold.it/250x100.png" custom-attr="custom1" another-attr="custom2" /><br> <img class="svg" src="https://upload.wikimedia.org/wikipedia/commons/0/02/SVG_logo.svg" alt="SVG" /><br> <img class="webp" src="https://gstatic.com/webp/gallery/1.webp" width="100" alt="webP" /><br> EOT; # Find all content with <img> tags preg_match_all( '/(?:<img|(?<!^)\G)\h*([-\w]+)="([^"]+)"(?=.*?\/>)/', $content, $images ); foreach ( $images[1] as $attributes[1] => $value ) { echo( '< img ' . $value . '="' . 'value' . '" ><br>' ); } //echo "<pre>"; print_r($images); $temp = array(); foreach($images[0] as $key=>$img){ $pos = strpos($img,'<img'); if($pos === false){ $temp[$key_2][] = $img; }else{ $temp[$key][] = $img; $key_2 = $key; } } foreach($temp as $k=>$v){ $str[] = implode(' ', $v) . ' />'; } $finalStr = implode('<br />', $str); echo $finalStr;

preferences:
25.77 ms | 406 KiB | 5 Q