I want ask what could be the mistake i am doing in this code. I am currently trying to find the first occurrence of an image tag or an object tag then return a piece of html if it matches one. Currently, I can get the image tag, but unfortunately I can't seem to have any results on object tag.
I am thought, I am doing some mistake in my regex pattern or something. Hope requirement is clear enough for you to understand thanks.
My code here:
function get_first_image(){
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches) || preg_match_all('/<object[0-9 a-z_?*=\":\-\/\.#\,<>\\n\\r\\t]+<\/object>/smi', $post->post_content, $matches);
$first_img = $matches [1] [0];
if(empty($first_img)){ //Defines a default image
$mediaSearch = preg_match_all('/<object[0-9 a-z_?*=\":\-\/\.#\,<>\\n\\r\\t]+<\/object>/smi', $post->post_content, $matches2);
$first_media = $matches2 [1] [0];
$first_img = "/images/default.jpg";
}
if(!empty($first_img)){
$result = "<div class=\"alignleft\"><img src=\"$first_img\" style=\"max-width: 200px;\" /></div>";
}
if(!empty($first_media)){
$result = "<p>" . $first_media . "</p>";
}
return $result;
}