08 January, 2009, 01:02:27 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: We Return!
 

Pages: [1]   Go Down
  Print  
Author Topic: Word Filter  (Read 2697 times)
0 Members and 1 Guest are viewing this topic.
http://Nevux.net

Demonic Pwns

Ex-Staff

Platinum Contribution
*
Hero Member

Karma: 3
Offline Offline

Gender: Male
Posts: 549
451089.59 Secksi Coins

View Inventory
Send Money to Slaytanist2

View Profile WWW
« on: 09 January, 2007, 01:44:44 AM »

Sup again. This is a tutorial on a basic word filter.

First we will make an array of a list of words we want to filter.
Code:
<?php

$filout 
= array("b!cth","d!ck","ass","fag","wtf");

?>

Now we have our array of badwords we want to filter out that other people might use.
Now lets make our function. (e.g function(){//content})
Code:
<?php

function filter($badwords){

$filout = array("b!cth","d!ck","ass","fag","wtf");

//content

}
?>

Now above we created our basic word listthat we wont to filter and now we have made our simple function with the parameter that we will use later.

in functions we use functions to change something in our code easier. They are very useful in time when you use/or do the same thing around your code.

now we will learn to foreach() and str_replace();

First str_replace(old,new,string);

"old" will be in quotes as the "old" text you are using basically it finds the word in the string and replaces it with the "new" text with something you want to change and string is the current string you are editing.

Now heres an example:
Code:
<?php

$old 
"nice";
$new "bad";
$string "Today was a {$old} day.";

$string str_replace($old,$new,$string);

echo 
$string;
?>

The above example would replace the old text with the new text: "Today was a bad day."

Now what foreach does is used when handling arrays that has different values but has the same variable name. Example:
Code:
<?
$hArray = array("1","2","3","4");

foreach($hArray as $output){
echo ("{$output}<br />");
}
?>
Lets explain line by line

Line 2: $hArray = array("1","2","3","4");
-well this simply gives us a variable with 4 different values making it a simple array that we declared.

Line 4: foreach($hArray as $output){
-Since $hArray has more then one value since its a array we want to show all its out puts right? well we say foreach value in this array as a new variable display that value.

Line 5:    echo ("{$output}<br />");

Now above will out put each # on a new line showing each value that was originally stored in the variable $hArray but now we declared it into a new variable making it show each value of the variable $hArray.

Now putting both together.


Code:
<?php

function filter($badwords){

$filout = array("bicth","dick","ass","fag","wtf");
$nobadwords "{No Cursing}";
foreach($filout as $replace){
$badwords str_replace($filout,$nobadwords,$badwords);
}
return $badwords;
}
?>

Now as you can see we did everything I showed you with putting foreach and str_replace together into a function.
Well now "return $badwords;" simplys returns the value using the function.

Live Example: http://phpdiscovery.com/tutorials/wordfilter.php

Source:

Code:
<style type='text/css'>
textarea{
width: 400px;
height: 400px;
}
</style>
<div align="center">
<?php

function filter($badwords){

$filout = array("bitch","dick","ass","fag","wtf","pussy","whore","gay","fuck");
$nobadwords "{No Cursing}";
foreach($filout as $replace){
$badwords str_replace($filout,$nobadwords,$badwords);
}
return $badwords;
}

if(!isset(
$_POST['s'])){

echo ("<form method='post'><textarea name='t'></textarea><input name='s' type='submit' value='Filter Example'></form>");
}else{

echo filter($_POST['t']);
}
?>
</div>
Logged


This resource was found useful by 2 out of 2 members. Rate it: [applaud] [smite]
Netherlands
B00!

O.o Me kodes...

Coding Team

Platinum Contribution
*
Hero Member

Karma: 47
Offline Offline

Gender: Male
Posts: 792
2191311.52 Secksi Coins

View Inventory
Send Money to Godkillah

View Profile
« Reply #1 on: 10 January, 2007, 03:45:42 PM »
Warning, virus detected. Please don't leave this page and wait for your computer to crash.

awsome! im gonna use this in future!
Logged

http://Nevux.net

Demonic Pwns

Ex-Staff

Platinum Contribution
*
Hero Member

Karma: 3
Offline Offline

Gender: Male
Posts: 549
451089.59 Secksi Coins

View Inventory
Send Money to Slaytanist2

View Profile WWW
« Reply #2 on: 10 January, 2007, 10:31:24 PM »

Thanks Smiley.
Logged


PanzerNew Zealand
Linux!

Administrator

Silver Contribution
*
Legendary Member

Karma: 42
Offline Offline

Gender: Male
Posts: 1463
10030288.59 Secksi Coins

View Inventory
Send Money to Panzer

View Profile WWW
« Reply #3 on: 12 January, 2007, 03:05:56 AM »

Nice tut Demonic!
Logged

Dragon HellfireUnited States
Ex-Staff

Bronze Contribution
*
Hero Member

Karma: 5
Offline Offline

Gender: Female
Posts: 844
548.00 Secksi Coins

View Inventory
Send Money to Dragon Hellfire

View Profile
« Reply #4 on: 15 January, 2007, 09:43:11 AM »

Nice tut, but I would of used a lot more comments in your code while you went along.
Logged

MS Paint >>> Photoshop!!!!
http://Nevux.net

Demonic Pwns

Ex-Staff

Platinum Contribution
*
Hero Member

Karma: 3
Offline Offline

Gender: Male
Posts: 549
451089.59 Secksi Coins

View Inventory
Send Money to Slaytanist2

View Profile WWW
« Reply #5 on: 24 January, 2007, 06:29:23 AM »

Well I explained alot O_o maybe ill edit with Comments soon.
Logged


Confused

Yeah...

Ex-Staff

Bronze Contribution
*
Hero Member

Karma: 3
Offline Offline

Gender: Male
Posts: 571
576.00 Secksi Coins

View Inventory
Send Money to death_oclock

View Profile
« Reply #6 on: 25 June, 2007, 04:33:30 AM »

Nice tutorial. Although, you can just pass the filter array into str_replace and it will iterate through it on its own. Although the way you did it also explained the foreach loop, which I personally wasn't very familiar with until now, so nice job.
Logged

I don't even know what i'm talking about, so don't ask.
Pages: [1]   Go Up
  Print  
 
Jump to:  
Sponsored Links:

Powered by MySQL
Powered by PHP
Powered by SMF 1.1.1 | SMF © 2005, Simple Machines LLC
TinyPortal v0.8.6 © Bloc
The rest copyright TCZ you pillocking twat!
Valid XHTML 1.0!
Valid CSS!
Places we'd like you to visit.