Remove profanity from some text
04-08-2008, 09:59 PM
|
Remove profanity from some text
|
Putting food on my family
Posts: 3,418
Name: Daniel
Location: A sleepy town in Mid Wales
|
I'm still a bit of a PHP noob. How do I remove certain words from a bit of text? I want to filter out profanity so it needs to search for multiple words and then replace these with **** (stars). It would be ideal if it would replace the words with the same number of stars as there are letters.
Does anyone have a function that can do this?
Thanks,
Dan
|
|
|
|
04-09-2008, 12:35 AM
|
Re: Remove profanity from some text
|
Posts: 984
Name: Jeremy Miller
Location: Reno, NV
|
Here's something I just came up with. Make sure that words containing other words in your list occur before the contained word.
PHP Code:
<?php $word_list = array("/(reallybad)/ei", "/(bad)/ei"); $string = "This sentence doesn't contain bad or reallybad words. Bad words shouldn't really be used by people unless they're reallybad people."; echo preg_replace($word_list,"preg_replace('/[a-z]/i','*','\\1')",$string); ?>
To see what I mean about "words contained within other words", see the difference in output of this version.
PHP Code:
<?php $word_list = array("/(bad)/ei", "/(reallybad)/ei"); $string = "This sentence doesn't contain bad or reallybad words. Bad words shouldn't really be used by people unless they're reallybad people."; echo preg_replace($word_list,"preg_replace('/[a-z]/i','*','\\1')",$string); ?>
|
|
|
|
04-09-2008, 02:40 PM
|
Re: Remove profanity from some text
|
Posts: 738
|
An alternative that I'm playing around with for my own personal amusement is instead of replacing the word with an asterisk, replacing it with a different word of your choice. For example:
would become:
In addition to censoring swear words, this method would give you the added enjoyment of making the swearer look like an idiot.
|
|
|
|
04-09-2008, 03:13 PM
|
Re: Remove profanity from some text
|
Putting food on my family
Posts: 3,418
Name: Daniel
Location: A sleepy town in Mid Wales
|
Quote:
Originally Posted by VirtuosiMedia
An alternative that I'm playing around with for my own personal amusement is instead of replacing the word with an asterisk, replacing it with a different word of your choice. For example:
would become:
In addition to censoring swear words, this method would give you the added enjoyment of making the swearer look like an idiot.
|
Lol. That is actually quite a good idea - at the moment, I'm trying to look around to see if I can download a table full of profane words so that I can run a check using a database rather than a PHP array - it would just make everything easier to manage in the longer term, and would also give me the option to do what you're saying.
Dan
|
|
|
|
04-09-2008, 03:20 PM
|
Re: Remove profanity from some text
|
Posts: 984
Name: Jeremy Miller
Location: Reno, NV
|
Quote:
Originally Posted by whym
I can run a check using a database rather than a PHP array - it would just make everything easier to manage in the longer term
|
I'm not sure that that is the best idea -- why have the overhead of database queries when they're not needed? You could always store a list of words in a text file, explode by newline, and construct the array as needed.
Quote:
Originally Posted by whym
would also give me the option to do what you're saying.
|
A modified version of the script I wrote would allow you to replace each word with it's own replacement. You'd just drop the "e" in the /ei and add an array of replacements for the second parameter to preg_replace.
As always, TK appreciated.
|
|
|
|
04-09-2008, 03:24 PM
|
Re: Remove profanity from some text
|
Putting food on my family
Posts: 3,418
Name: Daniel
Location: A sleepy town in Mid Wales
|
Yeh thanks Jeremy. I did actually implement your code earlier - it is quite useful. The database option is probably still my favourite though since I'm far more used to dealing with databases than PHP arrays or text files. Thanks anyway.
Dan
|
|
|
|
04-09-2008, 03:36 PM
|
Re: Remove profanity from some text
|
Posts: 984
Name: Jeremy Miller
Location: Reno, NV
|
Well, Dan, I will shed a tear for this code having to have come into existence, but here it is (untested, so the ORDER BY clause might not be so hot):
PHP Code:
<?php $string = "This sentence doesn't contain bad or reallybad words. Bad words shouldn't really be used by people unless they're reallybad people."; $bad_words = mysql_query("SELECT bad_word, new_word FROM bad_words ORDER BY CHAR_LENGTH(bad_word) DESC"); if ($bad_words && mysql_num_rows($bad_words) > 0) { while($a_word = mysql_fetch_object($bad_words)) { $string = preg_replace('/'.$a_word->bad_word.'/i', $a_word->new_word, $string); } } ?>
Once you've implemented that, please run a benchmark test: Create text of about 100k in size and populate both pieces of code with the same bad words and run them each, calculating how much time each script takes. You'll then see just how *** using the database is instead of the first method proffered (asterisks added simply for entertainment).
Let me just beat this dying horse into the ground: Your goal should really be to have your code run efficiently so that your server does less work and pages are served more quickly -- even if it means studying up a bit on what you're uncomfortable with. As an example, just last night I studied up on output buffering (which I always thought was for those who just couldn't organize their code efficiently) and found that coupled with gzip compression I was able to reduce bandwidth usage by quite a bit (2-3 times, to be exact). Took a few tutorials and a couple of tests, but I was able to get it and now my sites will be better for it.
Just saying...
Have an awesome day!
|
|
|
|
04-09-2008, 03:41 PM
|
Re: Remove profanity from some text
|
Putting food on my family
Posts: 3,418
Name: Daniel
Location: A sleepy town in Mid Wales
|
Thanks Jeremy. After all that, I'm not sure if i will use the database code or not now
I've spent a lot of today (apart from the time out for a rubbish haircut performed by an incompetent hairdresser) coding PHP and my brains beginning to shut down a bit, or I've lost my motivation to keep coding
Anyway thanks again.
Dan
|
|
|
|
04-09-2008, 04:01 PM
|
Re: Remove profanity from some text
|
Posts: 738
|
Just curious, Jeremy, but have you used the YSlow Firefox plugin? It might be worth a look at and Yahoo has a lot of good advice on compression.
|
|
|
|
04-09-2008, 04:04 PM
|
Re: Remove profanity from some text
|
Web 2.0? What is the Web?
Posts: 1,902
Name: <member type="brilliant" alt="foolish">James Lewitzke</member>
Location: / public_html / Universe / Virgo_Supercluster / Local_Group / Milky_Way / Orion_Arm / Solar_System / Earth / North_America / USA / Wisconsin
|
Quote:
Originally Posted by whym
I'm still a bit of a PHP noob.
|
You're good at stating the obvious, aren't you Dan?
(Just kidding  ..... You probably have at least 10X more PHP knowledge than I do  )
I just wanted to say here that if this has to do with your blog, you could always download the WordPress plugin for it:
http://wordpress.org/extend/plugins/...fytextreplace/
|
|
|
|
04-09-2008, 07:10 PM
|
Re: Remove profanity from some text
|
Posts: 984
Name: Jeremy Miller
Location: Reno, NV
|
Quote:
Originally Posted by VirtuosiMedia
Just curious, Jeremy, but have you used the YSlow Firefox plugin? It might be worth a look at and Yahoo has a lot of good advice on compression.
|
I haven't looked at that. Thanks for the recommendation. I usually optimize app structure, caching, processing routines, and SQL queries to increase efficiency and those are all things that a browser plugin wouldn't be able to help with. Also, it wouldn't have helped with the output buffering and gzip compression as those are server-side too. Never hurts to check another source, however.
|
|
|
|
04-09-2008, 07:11 PM
|
Re: Remove profanity from some text
|
Posts: 984
Name: Jeremy Miller
Location: Reno, NV
|
Quote:
Originally Posted by whym
After all that, I'm not sure if i will use the database code or not now 
|
Glad to hear it.
Quote:
Originally Posted by whym
I've spent a lot of today (apart from the time out for a rubbish haircut performed by an incompetent hairdresser) coding PHP and my brains beginning to shut down a bit, or I've lost my motivation to keep coding 
|
Happens to me all the time (the coding too long, that is -- I don't get out of the house enough to pay attention to haircuts, though the wife is good about complaining when I've gone too long).
|
|
|
|
04-09-2008, 08:13 PM
|
Re: Remove profanity from some text
|
Putting food on my family
Posts: 3,418
Name: Daniel
Location: A sleepy town in Mid Wales
|
Nope this isn't to do with my blog - this is something else I'm doing James. Thanks for suggesting the Wordpress plugin anyway.
Dan
|
|
|
|
04-09-2008, 10:47 PM
|
Re: Remove profanity from some text
|
Posts: 2
|
use preg_replace
|
|
 | |