Board index   FAQ   Search  
Register  Login
Board index PHP PHP General

Remplace bad words by others using str_replace function

General discussions related to php

Moderators: macek, egami, gesf

Remplace bad words by others using str_replace function

Postby howprotect » Thu Sep 13, 2012 2:07 am

Remplace bad words by others using str_replace function

As every one knows , some words like javascript , cookies …. are very harmful to our scripts and pose a threat to our websites security so the programmer must be very careful when he codes .

I have programmed a simple function that replace all words that i want by an other word , this function is bases on str_replace function( you can check it here http://www.php.net ) .

Note : this lesson is for mid-level so i won’t explain every thing in detail .
#Step One :

Open a new php file with name you want
#Step Two :

create a function with one parameter , i choose howprotect_bad_word as a name for my function and text as a name for the parameter

[php]
<?php

// All right are reserved to
// http://www.howprotect.net website
// to use function : howprotect_bad_word(here your text);
function howprotect_bad_word($text){

}

?>
[/php]
#Step Three :

we will create two variables , one contain an array for the bad words and another for the word that will replace the bad words.

the function become like this
[php]
<?php

// All right are reserved to
// http://www.howprotect.net website
// to use function : howprotect_bad_word(here your text);
function howprotect_bad_word($text){

$badword = array("script","hacker"); // choose the bad words
$newword ="<font color='red'>BadWord</font>";
// put the word that you want to replace the bad words

}

?>
[/php]
i choose two words to ban ” javascript and hacker ” , and i will replace it with BadWord in red color
#Break :
#Step Four :

now we will use the str_replace function

note : str_replace has 3 parameters ; str_replace(one,two,three)

-> one is for the the words that we will replace it

-> two is for the word that will replace the words in one

-> three is the text that we will replace it’s bad words (one ) with the new word (two )

i think that it’s clear now

now we finish the function
[php]
<?php
// All right are reserved to
// http://www.howprotect.net website
// to use function : howprotect_bad_word(here your text);
function howprotect_bad_word($text){
$badword = array("script","hacker"); // choose the bad words
$newword ="<font color='red'>BadWord</font>";
// put the word that you want to replace the bad words

$scan = str_replace($badword,$newword,$text);

return $scan ;
}
?>
[/php]
#Step Five :

to use this function just type this
1

echo howprotect_bad_word(your text here ) ;
#Finally :

the result of this function is
[php]
<?php
// All right are reserved to
// http://www.howprotect.net website
// to use function : howprotect_bad_word(here your text);
function howprotect_bad_word($text){
$badword = array("script","hacker"); // choose the bad words
$newword ="<font color='red'>BadWord</font>";
// put the word that you want to replace the bad words

$scan = str_replace($badword,$newword,$text);

return $scan ;
}
$paragraph="welcome to howprotect.net , with us you will protect you computer and script from hacker ";
echo howprotect_bad_word($paragraph);

?>
[/php]
is here

http://www.howprotect.net/wp-content/up ... 9/php1.png
howprotect
New php-forum User
New php-forum User
 
Posts: 1
Joined: Thu Sep 13, 2012 2:00 am

Return to PHP General

Who is online

Users browsing this forum: No registered users and 3 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.