That seems to remove any opening markup:
$str = '<html>< body><IMG src="xxxx" />text< img src="yyyy">more text<img src="zzzz" / ></ BODY></html>';
echo htmlentities(preg_replace( '#<.*img.*>#msiU', '', $str));
// Echos "textmore text</ BODY></html>"
Here's a function that will remove any html element or elements passed to it, along with its closing markup (if any). It will remove inappropiate spaces following opening brackets and closing slashes, and is case insensitive.
function remove_tag($str, $remove) // $remove can be scalar or array
{
while ((strpos($str, '< ') !== false) || (strpos($str, '/ ') !== false)) {
$str = str_replace(array('< ', '/ '), array('<', '/'), $str);
}
foreach ((array) $remove as $tag) {
$search_arr = array('<' . strtolower($tag), '<' . strtoupper($tag),
'</' . strtolower($tag), '</' . strtoupper($tag));
foreach ($search_arr as $search) {
$start_pos = 0;
while (($start_pos = strpos($str, $search, $start_pos)) !== false) {
$end_pos = strpos($str, '>', $start_pos);
$len = $end_pos - $start_pos + 1;
$str = substr_replace($str, '', $start_pos, $len);
}
}
}
return $str;
}
Demo:
$str = '<html>< body><IMG src="xxxx" />text< img src="yyyy">more text<img src="zzzz" / ></ BODY></html>';
echo '<pre>' . htmlentities(remove_tag($str, 'img')) . '</pre>';
// Echos "<html><body>textmore text</BODY></html>"
echo '<pre>' . htmlentities(remove_tag($str, 'body')) . '</pre>';
// Echos "<html><IMG src="xxxx" />text<img src="yyyy">more text<img src="zzzz" /></html>"
echo '<pre>' . htmlentities(remove_tag($str, array('img', 'body'))) . '</pre>';
// Echos "<html>textmore text</html>"