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.
<?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})
<?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:
<?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:
<?
$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.
<?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.phpSource:
<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>