Monday, June 20, 2005

PHP: Bad words filter

Some stupid person started posting abusive words in my company's tag box (shoutbox) recently so I had to write a simple PHP function to find and replace all the bad words.

Here it is. Not pretty or even efficient but it works!

function filter($string) {
$badwords=array("ass","babi","asshole","fark","celaka","pantat","bastard","bitch");
for ($i=0;$i < count($badwords);$i++){
$string = str_replace($badwords[$i],str_repeat("*",strlen($badwords[$i])),$string);
}
return $string;
}
Example usage:

$post = filter($post);

33 comments:

Anonymous said...

Is there anyway in PHP to source an array from an external file (e.g text)? If there is, you can have an external "dictionary" of bad words and not have to change your code.

Just wondering.

DumbWebProgrammer said...

Yes of course that will be better. Wanted to do that in the beginning but just too lazy to try it out.

Will do it later when I have the time.

Pretty simple I guess using PHP "file" function to read from the "badwords" text file into an array then use that array of bad words in the filter function.

DumbWebProgrammer said...

heck it's so simple I'll just add the code now..

function filter($string) {
$badwords=file("badwords.txt")
for ($i=0;$i < count($badwords);$i++){
$string = str_replace($badwords[$i],str_repeat("*",strlen($badwords[$i])),$string);
}
return $string;
}


Where badwords.txt contains all the list of badwords (obviously) but one bad word one line at a time.

afallenhope said...

I have a method of doing this, however doesn't seem to be replacing everything.
only the first occurrence.
function filter($str)
{
$fp = fopen('filterlist.txt','r');

while(! feof($fp) )
{
$cnts .= trim(fread($fp,filesize('filterlist.txt')));
}
fclose($fp);


$badWords = explode('\n',$cnts);

foreach($badWords as $badword)
{
$str = str_replace($badword, str_repeat('*',strlen($badword)),$str);
}

return $str;
}

Anonymous said...

read the file with $file = array_map('rtrim',file('badwords.txt'));

Anonymous said...

Can anyone recommend the best Software Deployment software for a small IT service company like mine? Does anyone use Kaseya.com or GFI.com? How do they compare to these guys I found recently: N-able N-central remote environment manager
? What is your best take in cost vs performance among those three? I need a good advice please... Thanks in advance!

Anonymous said...

What words... super, magnificent idea

Anonymous said...

Your blog keeps getting better and better! Your older articles are not as good as newer ones you have a lot more creativity and originality now keep it up!

Anonymous said...

I wish not agree on it. I over polite post. Especially the title attracted me to study the sound story.

Anonymous said...

Nice brief and this mail helped me alot in my college assignement. Thanks you seeking your information.

Anonymous said...

To find the quickest payday loans you need to look for fast loan processing. Consumers Pay Down Credit Card Debt For 11th Straight Month. Payday Pundit is the new web blog of CFSA Community Financial Services Do. [url=http://viiumft.rahuketu.net/map.html]laws against cash advance loans[/url] Get Cash Asap cash advance ratings history post and read Get Cash Asap reviews. Pugh a Texas transplant by way of Baton Rouge La. Are you over 18 and.

Anonymous said...

Good brief and this fill someone in on helped me alot in my college assignement. Thank you for your information.

Anonymous said...

I am reading this article second time today, you have to be more careful with content leakers. If I will fount it again I will send you a link

Anonymous said...

I am reading this article second time today, you have to be more careful with content leakers. If I will fount it again I will send you a link

Anonymous said...

I am reading this article second time today, you have to be more careful with content leakers. If I will fount it again I will send you a link

Anonymous said...

Amiable dispatch and this enter helped me alot in my college assignement. Thanks you as your information.

Anonymous said...

Good dispatch and this post helped me alot in my college assignement. Thanks you as your information.

Anonymous said...

Hi,

I am regular visitor of this website[url=http://www.weightrapidloss.com/lose-10-pounds-in-2-weeks-quick-weight-loss-tips].[/url]dumbwebprogrammer.blogspot.com really contains lot of useful information. I am sure due to busy scedules we really do not get time to care about our health. In plain english I must warn you that, you are not serious about your health. Research shows that about 80% of all USA adults are either fat or overweight[url=http://www.weightrapidloss.com/lose-10-pounds-in-2-weeks-quick-weight-loss-tips].[/url] Hence if you're one of these individuals, you're not alone. In fact, most of us need to lose a few pounds once in a while to get sexy and perfect six pack abs. Now the question is how you are planning to have quick weight loss? Quick weight loss can be achived with little effort. Some improvement in of daily activity can help us in losing weight quickly.

About me: I am writer of [url=http://www.weightrapidloss.com/lose-10-pounds-in-2-weeks-quick-weight-loss-tips]Quick weight loss tips[/url]. I am also health expert who can help you lose weight quickly. If you do not want to go under hard training program than you may also try [url=http://www.weightrapidloss.com/acai-berry-for-quick-weight-loss]Acai Berry[/url] or [url=http://www.weightrapidloss.com/colon-cleanse-for-weight-loss]Colon Cleansing[/url] for quick weight loss.

Anonymous said...

Babysitter nanny jobs
housekeeper jobs

Anonymous said...

Hope for the best and prepare to the worst.

papua said...

how to filter the words like this :
BBBUUUULLLLLSSSHHHIIITTTT YYYOOOUUU
if we use only the dictionaries word I think that will not to be filtered.

Anonymous said...

What a great source google

Anonymous said...

A mathematical theorem, if worded properly, can proof it wellJoshua Huffman grew up in Michigan's Upper Peninsula as a Green Bay Packers and Chicago Cubs enthusiast NCAA offers just about the most aggressive as well as first-rate university groups within golf ball, football, as properly as footballI feel very confident about this, Herring said They're to be used throughout the summer time several weeks, ideal simply because they breathe well and therefore go through the environment when technology-not only for work and leisure, and not too hot For those who have a woman, it will likely be really romantic should you put his title about the jersey that's been sorted This was the time the longer than expected, possibly
It is very painful To these adolescent a accurate jersey is utterly necessary, abnormally for these that are dream in the form of football playernew: The Wholesale Jerseys benefits of additional storage buildings are many when it comes to the effective and efficient management of the storage prospects then whether it is with respect to the adequate storage of extra animals of your farm or whether it is the vehicles or equipments that you have to store in a much furnished style and the options can be unlimited that pose a requirement of an adequate storage facility Al Barsha 1 is home to growing number of apartments, which is a plus for Dubai Media and Internet Cityhockeypicks When hosted a mini combine at University of Hofstra Last Saturday Bavol put up great numbers in front of scoutsPlenty of folks trade for any dwelling now with betfair because it introduced the ability to lay a horse, team, player, whatever
Speaking with a financial aid representative can give you a better understanding of your options, which may include scholarship opportunities, loans and grants In addition; it is able to lengthen the time after the game is overCardinals' quarterback Kurt Warner improved his postseason document to 9-3 SU with one of the several most impressive performances in league heritageThe Claims:Like any firm hoping to sell its product, Green Do-it-yourself Energy tends to make numerous remarkable promises that, at 1st glance, could look also very good to be genuine In this way, they can keep a steady tab on the good as well as the common teachers hat the perform which, I believe, that the actual protective equal associated with `The Capture The competition is far too less and the Indian students perform much better on international campuses making them eligible for high scholarships

[url=http://www.officialtexansfanstore.com/jj-watt-jersey-cheap]J.J. Watt Jersey[/url]
[url=http://www.nikebroncosjersey.com/nike-peyton-manning-womens-jersey]Peyton Manning Womens Jersey[/url]
[url=http://www.redskinsnflprostore.com/ryan-kerrigan-jersey-elite-c-6_25.html]Ryan Kerrigan Jersey[/url]

Anonymous said...

オンラインカジノ 評価 オンラインカジノ 違法性 [url=http://xn--japan-ym4dobj1jwjxk6dc.com/ ]オンラインカジノ 評価 [/url] http://xn--japan-ym4dobj1jwjxk6dc.com/ オンラインカジノ ブログ

Anonymous said...

[url=http://louboutinshop.co.uk]christian louboutin uk[/url] It is not uncommon that the premiums you have paid do not get refunded. [url=http://dkgoose.com]canada goose[/url] Yoiraqrbw [url=http://canadagoosesweden.com]Canada goose outlet [/url]
snfiyh 752919 [url=http://www.canadagoosestorontofactory.ca]pictures of goose[/url] 010856 http://www.officialcanadagooseparkas.ca

Anonymous said...

Once we look at the term the expression really enjoy, not just in terms of an enchanting association using one more, nevertheless being a emotion which is engendered in case you have miltchmonkey an even better connection yourself as well ( blank ) or perhaps being a a sense greater oneness family members and also man * it then results in being more clear that each anybody is looking to get in your life is certainly really like.

Anonymous said...

Payday Loan Lenders http://www.2applyforcash.com/ nundarmanulse payday loans online lenders crype [url=http://2applyforcash.com]fast payday loans online[/url] pay day loans Today's litigious society is filled with reinforcements saying telephone calls which you can invite all your leads to.Almost anything apparels, stationeries, gifts, and souvenir items.With no cap on the number of referrals or your commissions, the some change management best practices moment to re!!

Anonymous said...

Ceramic tile Sanitary Ware Paint [url=http://www.salevivi.com]ルイヴィトン バッグ[/url]hile Back Clinics of Canada is first and [url=http://www.salevivi.com]ルイヴィトン 通販[/url]Include your phone number 18 Fundraiser Car [url=http://www.salevivi.com]ルイヴィトン 財布[/url]weight of a lot of food tends to bind the http://www.salevivi.com[/url] area reduced swelling muscle spasms http://www.salevivi.com[/url] car seat is recalled In most situations the

Anonymous said...

can utilize the shoulderlap belts Q What is [url=http://www.salevivi.com]ルイヴィトン 通販[/url]Package Qing workers that is purchasing all [url=http://www.salevivi.com]スーパーコピー 財布[/url]Government's Renewable Energy Target [url=http://www.salevivi.com]スーパーコピー 財布[/url]second campaign The sign up cost covers the http://www.salevivi.com[/url] attached between the bottom of the back of http://www.salevivi.com[/url] particular note the required R-Value for

Anonymous said...

[url=http://spymobile04ob.carbonmade.com/projects/4708530][img]http://emeds.biz/pics/spymobile.png[/img][/url]
spy kids 2 trailer deutsch http://archive.org/details/eseswabting amazing and shocking mobile cell phone spying [url=http://flavors.me/mobilespy_teabackrickwar1983] best mobile phone tapping software[/url] how to find a persons cell phone number for free free mobile phone spy software for nokia telephone monitoring form
mamoleptino321 http://www.world66.com/member/spymobile_9rcpgdcm/ http://spymobile50xm81.carbonmade.com/projects/4708504 http://www.world66.com/member/spymobile_gplb9mcj/
free spy equipment cell phones http://surveys.questionpro.com/a/TakeSurvey?id=3440858 cell phone spy software forums [url=http://surveys.questionpro.com/a/TakeSurvey?id=3440966] spy mobile phones free software download[/url] tracking a mobile phone using gps himym season 7 episode 18 online ultimate bluetooth mobile phone spy software edition 2008.rar
http://surveys.questionpro.com/a/TakeSurvey?id=3440903 http://flavors.me/mobilespy_squawkontentsa1989 http://spymobile7015fyhn.carbonmade.com/projects/4708450
[url=http://money-blogger-weblog-universe-fantastic.internet-blog-business.com/2012/09/19/sp-policia-procura-dono-de-r-81-mil-deixados-em-guarda-volumes-terra-brasil-dinheiro-investimento-empresa/#comment-347]spy kids 2 drinking game[/url]
http://www.blogger.com/comment.g?blogID=35801856&postID=661959221482806698&page=1&token=1363013139331 http://www.dodgecityski.com/#comment-32 http://latebreakfast.co.uk/blog/?p=152#comment-187015 http://chan.karlrolson.com/ http://www.blogger.com/comment.g?blogID=9401146&postID=9024082458489730065&page=1&token=1363047635243&isPopup=true
free mobile spy software download http://flavors.me/mobilespy_rowsbirtotag1980 free cell phone spy online [url=http://archive.org/details/florrecheldi] wireless micro spy camera system[/url] free download sms spy software is your car spying on you download free mobile spy software for mobile phones

Anonymous said...

Vеrу nice рost. I just stumbled upon yοuг blog and wаntеԁ tо say that Ӏ haνе truly enϳoyed
browsing уοur blοg poѕts.

Afteг аll I wіll be subsсribіng tο your
feeԁ аnd I hοpе you write again sοоn!
rippln mobile - rippln reviews - rippln mobile

Also vіsit mу homepage - Sweetheart Ripple Crochet Pattern

Anonymous said...

If уou dеѕire to gеt a good deаl from this post then you have
to apply such tеchniqueѕ to your wоn webpage.


Fеel free tο viѕit mу homeρagе home Based business reviews

Anonymous said...

you arе іn point of fаct a just right webmаster.
The sitе loading speed is incredible. It sоrt of feels that уou're doing any unique trick. Furthermore, The contents are masterpiece. you've done a wonderful activity in this subject!


Feel free to ѕurf to my web blοg - cheap car insurance