Skip to content

Instantly share code, notes, and snippets.

@arryanggaputra
Last active September 3, 2015 09:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arryanggaputra/07f86b196aad97128ec4 to your computer and use it in GitHub Desktop.
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
<?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