This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function get_first_image($html) | |
{ | |
// We're trying to find and retrieve the SRC of the first image tag in the content | |
preg_match("/<img(.*)>/Uise", $html, $matches); | |
if(@trim($matches[1]) !== "") | |
{ | |
// Found an image tag! | |
// Get the SRC parameter ... | |
preg_match("/src=\"(.*)\"/Uise", $matches[1], $image); | |
// ... and return it | |
return $image[1]; | |
} | |
else | |
{ | |
// Return the path to an "image not found", or something similar | |
return "..."; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Credit goes to Wogan May: http://php.woganmay.com/2010/04/01/get-the-first-image-from-a-wordpress-post/