What is the ideal Php code for replacing ban words in a document with *** for middle characters while maintaining full break line display?
Keeping in mind that:
1- The input can have many lines with various line breaks and will come from TextArea
2- The changes are to be made to the Text before it is saved to MySQL table
3- So that when the Text is retrieved from MySQL it has the changed words
4- Ban words are to change like: “S!!!y” to “Sy" “F!!!P” to "FP”, so 3 stars is to replace the middle chars of Ban Words while keeping Case for 1st and last Chars
5- Ban words are in an Array
FYI, we have our code in this regard but it is not maintaining line breaks at all time perfectly, so I am looking for ideas from Php GODs here
Post the code you have, someone will offer an idea of how it could be improved. Off the top of my head, I’d suggest stripos() to find the dodgy words and a quick function to replace the centres with three asterisks, but that’s obviously not working for you. I don’t envy whoever has to maintain the list of new “bad” words.
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.
^ Indeed. Script can only do so much. If someone is determined to get around censoring it will require manual attention to keep things to a standard. Considering the very real possibility of both false positives and false negatives (eg. a word that might be bad in one context might be perfectly fine in a different context) I wouldn’t hope for perfection and would be prepared for manual intervention.
Well since no one has a better solution, better code, than what I posted for this rather normal ask in Php message processing. Then we went ahead with this code we had created. FYI, it works fine 99.99% of cases. Just in those rare cases that the Banned word is the last Word of a line, then it misfires. Which so rarely is the case, which is fine.
I guess this makes me one of the Php GODs
Since no one had better code for this task.
Are you saying that we send our Text messages to this resource outside of our company (server) for filtering banned Words? If yes, that is definitely something we cannot and will not do, due to various security concerns. Also keep in mind that one of our Req is that the:
Replace the letters in the Words matching ban words with ** in the middle Chars keeping in place the 1st and last char
Which this code does not seem to do at all.
So it seems like my Code remains the only choice! Hence I for sure one of Php Gods now
There were two examples of searches you could do yourself to find a pre-made solution:-
The first one involves APIs which you have dismissed.
The second involves libraries which may suit you better.
If Googling things for yourself is beyond the effort you are prepared to make, there were some library solutions linked in the topic I linked to in post #5 (which was a big part of the reason I linked to it).