homepage Welcome to WebmasterWorld Guest from 54.161.175.231
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
How to censor a string.
zozzen




msg:3567175
 10:21 pm on Feb 5, 2008 (gmt 0)

When people submit a form, I'd like to check if that string they submit include some unfavourable words.

For example, if username= "hello#*$!lovely", only "#*$!" is unfavourable, but "hello" and "lovely" are surely not in the banned list. Which function can i use to check it?

Thanks a lot!

 

GamingLoft




msg:3567184
 10:27 pm on Feb 5, 2008 (gmt 0)

ereg, its quite simple to do.

Check out the info at PHP.net

-ereg function [php.net]
-eregi function [php.net] (not case sensitive)

[edited by: GamingLoft at 10:28 pm (utc) on Feb. 5, 2008]

eelixduppy




msg:3567185
 10:28 pm on Feb 5, 2008 (gmt 0)

You can search with regular expressions [php.net] or a simple strpos [php.net]/stripos [php.net] call on the string to find any bad words. These words are usually stored somewhere like a database, however, you can just as easily keep these in an array in your application.

Duskrider




msg:3567190
 10:34 pm on Feb 5, 2008 (gmt 0)

I usually just stick mine in a text file and then read them into the program. That way if somebody gets extra creative all I have to do to add a new blocked word is open the text file and add a line.

joelgreen




msg:3567682
 2:46 pm on Feb 6, 2008 (gmt 0)

The problem is where to get the list :( Found some entries searching for "bad words list".

Here is related topic [webmasterworld.com...]

zozzen




msg:3567831
 5:29 pm on Feb 6, 2008 (gmt 0)

Thanks for all your help. As a newbie to PHP I'm still struggling to find out a solution to that. After reading the php manual provided above, Strpos seems to be the easiest way for me to go at this moment.

Here's my code...
-----------------------
Form :
<form method="POST" action="$_SERVER[PHP_SELF]">

Your name: <input type="text" name="username">
<input type="submit" value="submit">
</form>
---------------------
Censorship:
$censor_word = 'fuxxk'
$test = strpos($username, $censor_word);

if ($test === true) {
echo "Don't call yourslef '$censor_word'. Use a proper name";
}

-----------------------

It seems fairly inefficient with this method. I'll try to use array to include more censored words (use foreach function?) .

I'd like to follow Duskrider's suggestion to use a text file to include dozens of bad words in one file, but have no idea how to do it yet. I appreciate any hints for this.

Thanks!

menace_sa




msg:3569579
 12:09 pm on Feb 8, 2008 (gmt 0)

Other way is to use a str_replace method if you dont want to do all that, but you will have to check one "bad word" at a time.

Ex $username = str_replace("%","",$username);

This will check the string for the % sign and replace it with nothing.

dreamcatcher




msg:3569650
 2:25 pm on Feb 8, 2008 (gmt 0)

str_replace also supports arrays:

$string = str_replace(array('bad','bad2','bad3'),'',$string);

dc

Duskrider




msg:3569704
 3:35 pm on Feb 8, 2008 (gmt 0)

Here's how you would incorporate that with an external text file:

// Read the contents of 'badwords.txt' into an array
// trimming the newline characters as we do so.
$file = array_map('rtrim',file('badwords.txt'));

// Now just like Dreamcatcher said:
$string = str_replace($file,'*',$string);

That should look through your string and replace anything that looks like a line from the textfile with asterisks(*). If you wanted to replace it with something else, you could do that. If you don't care about case (you want to block Badword and badword and BaDWoRd but don't want to list each one in your text file individually) you should use str_ireplace instead.

zozzen




msg:3570457
 4:49 pm on Feb 9, 2008 (gmt 0)

Amazing explanations. Every time i thought i have learned enough syntax and function () in php to accomplish a particular task, I always found that i still have a lot to learn. Thanks for everyone's contribution and i begin to love programming.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved