I have a website that gathers images and most of the time it 开发者_开发知识库successfully gets an image but other times it renders an empty image tag like this <img src="" />
and the result is a red x
how do i use php to eliminate these empty image tags?
I don't want to eliminate all of the images, just the empty ones
When outputting images to display using PHP, I always use a conditional statement using the file_exists function. If the file exists, then display the image.
However if you already have the img string, then do a replace on the string.
// Put this before your empty imgs somewhere in your script
function myCb($buffer)
{
return str_replace('<img src="" />', '' , $buffer);
}
ob_start('myCb');
// *Your code*...
// Put this after your empty imgs in your script
ob_end_flush();
Please note that it's probably not the right way to handle this but this answer your question. I suggest not generating empty imgs at all since it would be x times faster.
精彩评论