You could shorten the code by using only one $replace. Variables are not needed too. Also you might want to use $active so it could be used in a PM.