Last active
September 3, 2015 09:34
-
-
Save arryanggaputra/07f86b196aad97128ec4 to your computer and use it in GitHub Desktop.
Get first image, if there's no image give the default Image URL
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
<?php | |
preg_match_all('~<img ([^>]+)>~i', $yourLongArticle, $matches); | |
$images = []; | |
foreach ($matches[1] as $str) { | |
preg_match_all('~([a-z]([a-z0-9]*)?)=("|\')(.*?)("|\')~is', $str, $pairs); | |
$images[] = array_combine($pairs[1], $pairs[4]); | |
} | |
if (!empty($images[0])) { | |
return $images[0]['src']; | |
} | |
return "your default image URL"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment