Skip to content

Instantly share code, notes, and snippets.

@Maden-maxi
Last active December 16, 2016 16:53
Show Gist options
  • Save Maden-maxi/87a56e4c2c94fbc48796929613edf880 to your computer and use it in GitHub Desktop.
Save Maden-maxi/87a56e4c2c94fbc48796929613edf880 to your computer and use it in GitHub Desktop.
/**
* @param string $str Text to find <img> src attribute value
* @param string(optional) $placeholder if <img> src attr is empty use $placeholder
* @param boolean(optional) $echo - if true display value else return value
* @return string
*/
function ecf_find_img_url($str, $echo = false, $placeholder = 'http://placehold.it/350x150' ) {
$start = stripos($str, '<img');
if($start !== false){
$str = substr($str, $start);
$end = stripos($str, '>');
$str = trim( substr($str, 0, $end+1) );
$start = stripos($str, 'src="');
$end = stripos($str, '>');
$str = substr($str, $start+strlen('src="'), $end);
$end = stripos($str, '"');
$str = substr($str, 0, $end );
if($echo){
echo $str;
} else {
return $str;
}
} else {
if($echo){
echo $placeholder;
} else {
return $placeholder;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment