Anziche usare str_replace, basta che usi preg_replace o ereg_replace, che hanno le espressioni regolari