Skip to content

Instantly share code, notes, and snippets.

@wesbillman
Created June 2, 2010 16:07
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 wesbillman/422591 to your computer and use it in GitHub Desktop.
Save wesbillman/422591 to your computer and use it in GitHub Desktop.
<?php
function get_image_from_content($content) {
// Define the pattern to search
$szSearchPattern = '~<img [^\>]*\ />~';
// Run preg_match_all to grab all the images and save the results in $aPics
preg_match_all( $szSearchPattern, $content, $aPics );
// Count the results
$iNumberOfPics = count($aPics[0]);
// Check to see if we have at least 1 image
if ( $iNumberOfPics > 0 )
{
return $aPics[0][0];
};
return "<img class="default" src='".bloginfo("template_directory")."'/images/default.jpg' alt='Photo Coming Soon' width='60' height='60' />";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment