Hightlight or Censor Words in PHP

by Darren Hedlund
Microcyb.com
Monday, 28th November 2005

This tutorial will show you how to load a flat file and parse text to highlight or even censor out a word.

First step is to create an empty folder and create two text files.  The first one rename it to badwords.txt, and the second as index.php

Open the badwords.txt file and enter in:
dog
fleas

Next open the index.php file, and past the following information

<?php
$message = "My dog has fleas....";
censor($message);
function censor($message)
 { 
 $fh = fopen("badwords.txt","r"); //Open the file that contains the words
 while($word = fgets($fh,4096))
  {
  $message = ereg_replace(trim($word),"<b>$word</b>",$message); //replace words
     }
 return $message; //return censored words
 } 
echo censor($message);
?>
  

Access the index.php through your localhost address, or web page, abd you should see the following:

My dog has fleas....


Options:
Printer Friendly

About The Author:

Darren Hedlund is a freelance Web developer, writer, and data analyst. Darren has a graduate degree in Computer Information Science and has spent the last 15 years developing application and environments from hand held, windows, web, virtual science, gaming, artificial intelligence and graphics design. Darren's coding knowledge ranges from C+, Visual Basic, .NET, PHP, JSP, REXX, KIXX, and many others. His graphical and environmental knowledge stems in Macromedia Flash, 3D studio Max, Curious Labs Poser, Adobe Photoshop, and many others. Darren works in many platforms ranging from database, visual design, and, system development.

Developer Categories



Developer Tutorials
ASP
CGI & Perl
CSS
Flash
HTML
Java
JavaScript
MySQL
PHP
Python
XML

Developer Documentation

Developer Tools



Search our Developer Tutorials
  The DevSyndicate Network