Jump to content

[SOLVED] OOP basic language filter


MasterACE14

Recommended Posts

I've read a few tutorials on PHP OOP, tried out a few examples, and its making sense so far. I'm trying to make a language filter in OOP, and nothing appears to be happening.

 

I have a text file that has the curse words in it. And I check the curse words, against input from a text area and replace any curses with "****"

However, currently, I put in curse words, it just echo's them without replacing them. I'm not sure whether the problem is with my class, or with my actual PHP code.

 

Any help is greatly appreciated.

 

curses.txt

curse1
curse2
curse3
curse4

 

example.php

<?php
include("class.inc.php");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Example</title>
</head>
<body>

<?php

$form = <<<_FORM
<form action="example.php" method="post">
<textarea rows="5" cols="20" wrap="physical" name="text">
</textarea>
<input type="submit" value="submit!" />
</form>
_FORM;

if(isset($_POST['text']) && strlen($_POST['text']) > 0) {
// parse the text input for profanity
$text = new filter($_POST['text']);
// return the filtered text
echo $text->get_text();

} else {
// echo "Nothing to Parse!";
echo $form;
}

?>

</body>
</html>

 

class.inc.php

<?php

class filter {

var $text;
var $debug;

function __construct($text,$debug=false) {

	$this->text = $text;
	$this->debug = $debug;

}	

function filter_text($input_text) {
	// check there is a list of curse words
	if(file_exists("curses.txt")) {
		$curses = file("curses.txt");
		$this->text = substr_replace($curses,"****");	
	} else {
		trigger_error("No curse word file to read from!",1);
	}

}

function get_text() {

	if($this->debug == true) {
		// run a check if there is curse words, then say there is
		return "Debug Mode is Enabled";
	} else {
		// just return the filtered text
		return $this->text;
	}

}

}

?>

 

Regards, ACE

Link to comment
https://forums.phpfreaks.com/topic/126578-solved-oop-basic-language-filter/
Share on other sites

How are you calling the class? Because it seems you have a method that takes input but does nothing with it

"function filter_text($input_text) {..."

 

You don't at any time call the filter_text() method, therefore your text is NOT being filtered... All you're doing at the minute is setting an internal variable, and then outputting it.

 

I've made some adjustments, and I think my OOP is fine now, please correct me if I'm wrong. But I'm now getting a PHP error.

 

here's my error:

Warning: Wrong parameter count for substr_replace() in C:\Program Files\Abyss Web Server\htdocs\form_language_filter\class.inc.php on line 20

 

 

and here's the new files...

 

example.php

<?php

$form = <<<_FORM
<form action="example.php" method="post">
<textarea rows="5" cols="20" wrap="physical" name="text">
</textarea>
<input type="submit" value="submit!" />
</form>
_FORM;

if(isset($_POST['text']) && strlen($_POST['text']) > 0) {
// parse the text input for profanity
$text = new filter($_POST['text'],false);
// return the filtered text
echo $text->filter_text();

} else {
// echo "Nothing to Parse!";
echo $form;
}

?>

 

class.inc.php

<?php

class filter {

var $text;
var $debug;

function __construct($text,$debug=false) {

	$this->text = $text;
	$this->debug = $debug;

}	

function filter_text() {

	// check there is a list of curse words
	if(file_exists("curses.txt")) {
		$curses = file("curses.txt");
		$this->text = substr_replace($curses,"****");	
	} else {
		trigger_error("No curse word file to read from!",1);
	}

	if($this->debug == true) {
		return "Debug Mode is Enabled";
	} else {
		// just return the filtered text
		return $this->text;
	}	

}

}

?>

That would be exactly what the error says ;)

 

You're using substr_replace() with arrays, which is fine, it'll let you do that, HOWEVER you must specify the same number of elements in both the 1st and 2nd array inputs.

e.g. substr_replace(array(1,2), array(2,1), $subject).

 

See http://www.php.net/substr_replace for a reference. You're providing an incorrect number of parameters.

 

substr_replace() requires a something to search for (param 1), something to replace it with (param 2) and the text to do it in (param 3).

substr_replace($search, $replace, $subject);

Success, I changed the code around in class.inc.php yet again but have the language filter working perfectly now! Thankyou for your excellent help aschk! much appreciated!  ;D

 

final class.inc.php

<?php

class filter {

var $text;
var $debug;

function __construct($text,$debug=false) {

	$this->text = $text;
	$this->debug = $debug;

}	

function filter_text() {

	// check there is a list of curse words
	if(file_exists("curses.txt")) {
		// curses list
		$filename = "curses.txt";
		// open curses.txt
		$openfile = fopen($filename,"r");
		// grab contents
		$contents = fread($openfile,filesize($filename));
		// close curses.txt
		fclose($openfile);
		// explode the contents
		$curses = explode(", ", $contents);
		// replace curse words
		$this->text = str_replace($curses,"****",$this->text);
	} else {
		trigger_error("No curse word file to read from!",1);
	}

	if($this->debug == true) {
		return "Debug Mode is Enabled";
	} else {
		// just return the filtered text
		return $this->text;
	}	

}

}

?>

sorry for double posting, but I've made 2 more changes... now I'm done  :)

 

the changes are...

- it now filters out words from curses.txt in both upper and lower case.

- words in curses.txt can now be put in 1 line each...

curse1

curse2

curse3

curse4

etc etc

 

class.inc.php:

<?php

class filter {

var $text;
var $debug;

function __construct($text,$debug=false) {

	$this->text = $text;
	$this->debug = $debug;

}	

function filter_text() {

	// check there is a list of curse words
	if(file_exists("curses.txt")) {
		// curses list
		$filename = "curses.txt";
		// open curses.txt
		$openfile = fopen($filename, "r");
		// grab contents
		$contents = fread($openfile, filesize($filename));
		// close curses.txt
		fclose($openfile);
		// explode the contents - grab it line by line
		$curses_lower = explode("\r\n", $contents);
		$curses_upper = explode("\r\n",strtoupper($contents));
		// replace curse words - lower case
		$this->text = str_replace($curses_lower,"****",$this->text);
		// replace curse words - upper case
		$this->text = str_replace($curses_upper,"****",$this->text);
	} else {
		trigger_error("No curse word file to read from!",1);
	}

	if($this->debug == true) {
		return "Debug Mode is Enabled";
	} else {
		// just return the filtered text
		return $this->text;
	}	

}

}

?>

 

Thanks again for your help aschk.

 

Regards ACE

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.