Don't use PHP for this unless you have the CGI version installed.
Use the file() function to break the .htpasswd files into lines, then for each .htpasswd file, loop thru each line and do a string comparison, perhaps strstr().
eg:
<?
if ($lines=file($htpasswdfile))
while (list($k, $line)=each($lines))
if in_array($badlist, $line))
unset($lines[$k]);
reset($lines);
while (list($k, $v)=each($lines))
$write.=$k."\n";
$fp=fopen($htpasswdfile, 'w');
fwrite($fp, $write);
fclose($fp);
?>
Go for it! This is an exercise in PHP's strengths as a string processing engine.