<?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;
- Output for 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.25, 8.4.1 - 8.4.12
- < img src="value" ><br>< img alt="value" ><br>< img src="value" ><br>< img alt="value" ><br>< img src="value" ><br>< img alt="value" ><br>< img class="value" ><br>< img src="value" ><br>< img alt="value" ><br>< img src="value" ><br>< img custom-attr="value" ><br>< img another-attr="value" ><br>< img class="value" ><br>< img src="value" ><br>< img alt="value" ><br>< img class="value" ><br>< img src="value" ><br>< img width="value" ><br>< img alt="value" ><br><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" />
preferences:
125.96 ms | 411 KiB | 5 Q