Skip to content

Instantly share code, notes, and snippets.

@bigandy
Last active August 29, 2015 14:17
Show Gist options
  • Save bigandy/7189c719c5664ca444af to your computer and use it in GitHub Desktop.
Save bigandy/7189c719c5664ca444af to your computer and use it in GitHub Desktop.
Resp Images Find strings in string, order independent.
// original HTML, when added to post
<img src="http://boilerplate.dev/wp-content/uploads/2015/03/spiral_01.jpg" alt="spiral_01" width="993" height="1000" class="alignnone size-full wp-image-45" />
// when change the class, order of HTML is changed.
<img class="alignnone wp-image-45 size-full" src="http://boilerplate.dev/wp-content/uploads/2015/03/spiral_01.jpg" alt="spiral_01" width="993" height="1000" />
Need to get:
1. class
i. the number 45 from the end of wp-image-45
ii. the full class e.g. alignnone wp-image-45 size-full
2. the height
3. the width
So far I have :
// this gets the whole of the image tag e.g. '<img src="http://boilerplate.dev/wp-content/uploads/2015/03/spiral_01.jpg" alt="spiral_01" width="993" height="1000" class="alignnone size-full wp-image-45" />'
preg_match_all( "/<img (.*?)\/>/", $content, $matches );
foreach ( $matches[0] as $key ) {
// gets the width
preg_match( '/(.*?)width=\"((\d+))\"(.*?)/', $key, $width );
// gets the height
preg_match( '/(.*?)height=\"((\d+))\"(.*?)/', $key, $height );
// gets the class
preg_match( '/(.*?)class=\"((.*?)wp-image-(\d+)(.*?))\"(.*?)/', $key, $class );
$id = $class[4];
$output_class = $class[2];
$height = $height[2];
$width = $width[2];
// the string with <img>
$img = $key;
// string with <img srcset>
$resp_img = es_get_output_resp_image( $id, $output_class, $max_width, true, $width, $height );
// replace <img> with <img srcset...>
$content = str_replace( $img, $resp_img, $content );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment