A
Anonymous
Guest
I am in the process of learning OOP by reading "PHP Advanced and Object-Oriented Programming" by Larry Ullman (BTW - In my opinion a very good book to further your skills in php). An decided to test out my newly learned programming skills on a very simple Class that replaces 'dirty' words with 'clean' words. Like I said this is a very simple dirty word checker that can easily be thwarted by misspelling the words intentionally or other clever ways that we have all seen. I guess though it could be improved upon, but in my opinion it would be a losing battle.
Anyways here's the class the I called DirtyWord.php
This is only a partial code of my edit_page.php file, It uses quickform2 form PEAR package that the author uses and that I plan on using for it's a very neat little package. Anyways I think my comments are pretty much self-explanatory, so I will leave you with that:
I could had made it static, but I guess the preference is up to the coder and I chose to do it that way. 8)
I might in the future modify this class to do other things, but from what I'm learning is to write your classes as simply as possible. Since I now am using an auto-loader, I don't have to worry about having to include the files.
Here's the auto-loader script for classes:
Pretty simple right?
Take care,
The Pepster
Anyways here's the class the I called DirtyWord.php
Code:
<?php # DirtyWord class version 1.0 beta
// DirtyWord is a class.
class DirtyWord {
public $checkTitle;
public $checkContent;
/* This constructor sets up two variables though it could work fine with just one,
* but since it is written for a blog website I thought this would make better sense.
*/
function __construct($data) {
$this->checkTitle = $this->bad_word_filter($data['title']);
$this->checkContent = $this->bad_word_filter($data['content']);
}
protected function bad_word_filter($data) {
// Simple Bad Word Filer Written in Object-Oriented Style.
// Simply stops & replaces the offending word from being uploaded to the database.
// ***Feel Free to Use or Modify It.
// It's not very sophisticated for it won't catch purpose mispellings
// of bad words.
// Bad Words go into $patterns array
$patterns[0]= "****";
$patterns[1]= "****";
$patterns[2] = "*******";
$patterns[3] = "****";
$patterns[4] = "****";
$patterns[5] = "****";
$replacements[0] = "fiddlesticks";
$replacements[1] = "hockeypuck";
$replacements[2] = "scatterbrain";
$replacements[3] = "dread";
$replacements[4] = "homer";
$replacements[5] = "prostitute";
return $this->user_edits = str_ireplace($patterns, $replacements, $data); //replace it
}
}
This is only a partial code of my edit_page.php file, It uses quickform2 form PEAR package that the author uses and that I plan on using for it's a very neat little package. Anyways I think my comments are pretty much self-explanatory, so I will leave you with that:
Code:
$form->addRecursiveFilter('trim');
// Process the data if form is validated:
if ($form->validate()) {
// Update the edited text:
$query = 'UPDATE pages
SET creatorId=:creatorId,
title=:title,
content=:content,
dateUpdated=NOW()
WHERE id=:id';
$stmt = $pdo->prepare($query);
// Clean-up user content:
// Setup an array - 'title' and 'content' are the keys:
$data = array('title' => $title->getValue(), 'content' => $content->getValue());
// Create a new instance:
$dirtyWord = new DirtyWord($data);
// Check the content for bad language:
$title->setValue($dirtyWord->checkTitle);
$content->setValue($dirtyWord->checkContent);
$result = $stmt->execute(array(':creatorId' => $user->getId(), ':title' => $title->getValue(), ':content' => $content->getValue(), ':id' => $page->getId()));
// Freeze the form upon success:
if ($result) {
$form->toggleFrozen(true);
$form->removeChild($submit);
}
}
I could had made it static, but I guess the preference is up to the coder and I chose to do it that way. 8)
I might in the future modify this class to do other things, but from what I'm learning is to write your classes as simply as possible. Since I now am using an auto-loader, I don't have to worry about having to include the files.
Here's the auto-loader script for classes:
Code:
// Autoload classes from "classes" directory:
function class_loader($class) {
require('classes/' . $class . '.php');
}
spl_autoload_register('class_loader');
Take care,
The Pepster