You just placed your code in the wrong place. You need to reset rank to nothing after it's been created, so add the code at the end.
PHP Code:
if (($post[usergroupid] ==5) || ($post[usergroupid] ==8)) {
$rank = "";
}
Or you could probably limit the rank to just the "Registered" group like this (replace XX with number fo your Registered group):
PHP Code:
if ($post[usergroupid] ==XX) {
--Entire Original Hack Code--
}