Php code to replace bad words in a document with *** and maintain break line

I would think using str_ireplace or maybe preg_replace (if you need something more complex) would be more efficient for this as they will take arrays directly as inputs saving you from all the explode, implode and loops you are using and do the replacemnt for you within the single function.

Though these things can be more complex than you may first think, when you start to consider false positives and the methods people may use to circumvent your efforts.