You are here: swear filter « All PHP « IT news, forums, messages
swear filter

Posted by Mike on 07/19/06 08:23

I have developed a script to filter swear words by reading a text file
that contains swear words and replace it with **** as follows...

test.txt:

bad1
bad2
bad3
anotherbad2

filter.php:

<?php
$textarea = "bad1 anotherbad2 bad3";
$filename = "test.txt";
$fp = fopen($filename, "r") or die("File not there");
while (!feof($fp)) {
$line = fgets($fp, 1024);
$wordlen = strlen(trim($line));
$count = 1;
$newword = "";
while ($count <= $wordlen) {
$newword .= "*";
$count++;
}
echo "Line = $line<br>";
echo "New Line = $newword<br>";
$textarea = preg_replace('/\b'.$line.'\b/i', $newword, $textarea);
}
echo $textarea;
?>

Below is the output I get....

Line = bad1
New Line = ****
Line = bad2
New Line = ****
Line = bad3
New Line = ****
Line = anotherbad2
New Line = ***********
bad1 *********** bad3

For some reason it only does the replace on the last work in the list
within test.txt, in this case "anotherbad2".

I update $textarea after every line in the while loop so I don't
understand.

Can anyone help?

Thanks

Mike

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация