Skip to content

Instantly share code, notes, and snippets.

@ethanosullivan
Last active April 7, 2017 11:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ethanosullivan/8d2c6d85193dfbe328ad2949fe06e9e5 to your computer and use it in GitHub Desktop.
Save ethanosullivan/8d2c6d85193dfbe328ad2949fe06e9e5 to your computer and use it in GitHub Desktop.
<?php
# Sample images
$content = <<<EOT
<p>List of sample images.</p>
<img src="https://placehold.it/250x100.jpg" width="250" height="100" alt="Dimensions Set" />
<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 );
// var_dump( $images );
$temp = array();
foreach ( $images[0] as $key => $img ) {
$position = strpos( $img, '<img' );
if ( $position === FALSE ) {
$temp[$key_2][] = $img;
}
else {
$temp[$key][] = $img;
$key_2 = $key;
}
var_dump( $temp );
}
foreach ( $temp as $key => $value ) {
$str[] = implode( '', $value ) . '>';
}
/*$finalStr = implode( '<br>', $str );
echo $finalStr;*/
if ( ! in_array( 'width', $img[1] ) || ! in_array( 'height', $img[1] ) ) {
list( $width, $height ) = getimagesize( str_replace( "\"", "" , $src ) );
}
// var_dump( $str );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment