[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 214
  • Last Modified:

Bad words filter class problem

Here is my predicament
I do not need to replace the bad words by any random signs
What I am trying to achieve is to make those bad words appearing in <font color=red>
At this point the class retrieves the bad words and replaces the bad words by the word array (only positive the word array comes in red!)
How may I replace the word array with the actual bad word?
Thanks

<?php
// an example
 
include("badwords.php");
 
// FOR POSTING BADWORDS= xxx
$content = "xxx any content xxx with bad words xxx";
 
$badword = new badword();
echo $badword->word_filter("$content");
//////// ends example /////////////
?>
<?php
//////// usage //////////
// for posting badwords = xxx
$badwords = array (
	"xxx",
	" xxx ",
	"xxx"
	);
 
$wordreplace = 
	array("<font color='red'>$badwords</font> ")	;	
	
// class
class badword {
	function word_filter($content) {
	global $badwords, $wordreplace;
	   $count = count($badwords);
	        for ($n = 0; 
		   	$n < $count; ++$n, next ($badwords)) {
			$filter .="$wordreplace[$n]";
		     $search = "$badwords[$n]";
		   $content = preg_replace("'$search'i","$filter",$content);
 }
 
	   return $content;
	}
}
?>

Open in new window

0
array007
Asked:
array007
  • 4
  • 4
1 Solution
 
gunny051499Commented:
Hi Array07,

look at the attached code snippet. I guess that code's a bit cleaner than the original one ;-)
Anyways - I hope I got your question right and this will do what you need.

The Class-Constructor is implemented in PHP5-fashion. So if you are running this on PHP4 you will need to rename "__construct" to "MarkupBadwords" (giving the init-method the name of the class).

Cheers

Gunny
<?php
 
/*
 * PHP5 class!
 */
class MarkupBadwords {
  protected $badwords = array();
 
  public function __construct( $initValues ) {
    if ( is_array( $initValues ) ) {
      // save array to array
      $this->badwords = $initValues;
    } else {
      // save single element to new, single array element
      $this->badwords[] = $initValues;
    }
  }
 
  public function word_filter( $content, $prefix, $postfix) {
    // loop through the badwords array
    for ($i = 0; $i < count($this->badwords); $i++ ) {
      $content = preg_replace("'".$this->badwords[$i]."'i", $prefix.$this->badwords[$i].$postfix, $content);
    }
    return $content;
  }
}
 
 
$markup = new MarkupBadwords( array("xxx", " xxx ", " xxx") );
 
$content = "xxx any content xxx with bad words xxx";
echo $markup->word_filter($content, "<font color='red'>", "</font>") . "\n";
 
?>

Open in new window

0
 
array007Author Commented:
Hello qunny,
Thanks for your help
Help me understand why it does not work by using two scripts/page
it results in
<<<
Warning: Missing argument 1 for MarkupBadwords::__construct(),
>>>>
I mean on one script having the badwords array and the class
then on a second page
include("badwords.php");
$content = "xxx any stufs xxx xxx";
$markup = new MarkupBadwords(); // with out the array spelled out as in your example
echo $markup->word_filter($content, "<font color='red'>", "</font>") . "\n";
?>
futher I am (sorry!) adding an additional question
as you probly noticed all those scripts find the badwords but not 100% corectly, they also find the "badwords" withing a correct word
for example in "assume" ... see where I go with that :)
so how could we, shy of having three verssions of the same bad word, isoloating only whole words and not section of
this works but is not efficient:
array(
"aaa",
" aaa",
"aaa "
);
as such one avoid grabing only a section of a word and if the word is the array first word or again if is the array last word but this in not efficient (undstatement)
0
 
gunny051499Commented:
Hi Array07,

first off, please notice that I changed the class to have a constructor which takes the badwords array as a parameter. This makes it possible to have different classes with different badwords without using global arrays, which is somewhat ugly and makes no sense when using classes!

You need to change

$markup = new MarkupBadwords();

to

$markup = new MarkupBadwords( array("xxx", " xxx ", " xxx") );

Second, matching whole words only is no trouble with regular expressions. Just test the attached script. I added an optional 4th parameter "wholeWordsOnly" (true=default / false).

Cheers

Gunny

<?php
 
/*
 * PHP5 class!
 */
class MarkupBadwords {
  protected $badwords = array();
 
  public function __construct( $initValues ) {
    if ( is_array( $initValues ) ) {
      // save array to array
      $this->badwords = $initValues;
    } else {
      // save single element to new, single array element
      $this->badwords[] = $initValues;
    }
  }
 
  public function word_filter( $content, $prefix, $postfix, $wholeWordsOnly = true) {
    if ( $wholeWordsOnly === true ) $b = '\b';
    // loop through the badwords array
    for ($i = 0; $i < count($this->badwords); $i++ ) {
      $content = preg_replace("'".$b.$this->badwords[$i].$b."'i", $prefix.$this->badwords[$i].$postfix, $content);
    }
    return $content;
  }
}
 
 
$markup = new MarkupBadwords( array("xxx", " xxx ", " xxx") );
 
$content = "xxx any content xxx with bad words xxx while xxxcorrectword is good";
echo $markup->word_filter($content, "<font color='red'>", "</font>") . "\n";
 
?>

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
array007Author Commented:
Hi  qunny
This is exactly what I did before posting but it still is missing an argument
here is what I have (using the previous script, not the last matching the whole word, and thanks for the whole word addition!)
/// page one badwords.php
$badwords = array ( (class and bad words array)
      "xxx",
      " xxx ",
      "xxx "
      );

      class MarkupBadwords {
  protected $badwords = array();
 
  public function __construct( $initValues ) {
    if ( is_array( $initValues ) ) {
      // save array to array
      $this->badwords = $initValues;
    } else {
      // save single element to new, single array element
      $this->badwords[] = $initValues;
    }
  }
 
  public function word_filter( $content, $prefix, $postfix) {
    // loop through the badwords array
    for ($i = 0; $i < count($this->badwords); $i++ ) {
      $content = preg_replace("'".$this->badwords[$i]."'i", $prefix.$this->badwords[$i].$postfix, $content);
    }
    return $content;
  }
}


/// page two

 include("badwords.php");
 
$markup = new MarkupBadwords();
 
$content = "xxx any content xxx with bad words xxx";
echo $markup->word_filter($content, "<font color='red'>", "</font>") . "\n";
0
 
gunny051499Commented:
Hi Array07,

nope - sorry - you are still initializing the class without the array (count 3 lines from bottom up in your post)!

WRONG: $markup = new MarkupBadwords();
CORRECT: $markup = new MarkupBadwords( array("xxx", " xxx ", " xxx") );

you could also do

$badwords = array ("xxx", " xxx ", "xxx ");
$markup = new MarkupBadwords( $badwords );

BTW. with the word-boundaries you don't need to put "xxx" in 3 times with spaces in front and at the end!

Cheers

Gunny
0
 
array007Author Commented:
Hi Gunny,
Thanks
I did not explain it well
I would like not to instant the class by populating the array as in your code
CORRECT: $markup = new MarkupBadwords( array("xxx", " xxx ", " xxx") );
I would like to be able to use
CORRECT: $markup = new MarkupBadwords($badwords);
it sound more compact and readable to me.
is it possible without going back to GLOBAL
thanks
0
 
array007Author Commented:
Thanks so much, sorry I did not read well the last post
great job :)
0
 
gunny051499Commented:
Well sure Array07, as in the example above, fill the badwords array first and then pass that array as a parameter when creating the class:

$badwords = array ( "badword1", "notgoodword2", "whatever3" );
$markup = new MarkupBadwords( $badwords );

Find the whole example attached!

Thanks for the points

Gunny
<?php
 
/*
 * PHP5 class!
 */
class MarkupBadwords {
  protected $badwords = array();
 
  public function __construct( $initValues ) {
    if ( is_array( $initValues ) ) {
      // save array to array
      $this->badwords = $initValues;
    } else {
      // save single element to new, single array element
      $this->badwords[] = $initValues;
    }
  }
 
  public function word_filter( $content, $prefix, $postfix, $wholeWordsOnly = true) {
    if ( $wholeWordsOnly === true ) $b = '\b';
    // loop through the badwords array
    for ($i = 0; $i < count($this->badwords); $i++ ) {
      $content = preg_replace("'".$b.$this->badwords[$i].$b."'i", $prefix.$this->badwords[$i].$postfix, $content);
    }
    return $content;
  }
}
 
 
$badwords = array ( "badword1", "notsogoodword2", "whatever3" );
$markup = new MarkupBadwords( $badwords );
 
 
$content = "xxx any content xxx with bad words xxx while xxxcorrectword is good";
echo $markup->word_filter($content, "<font color='red'>", "</font>") . "\n";
 
?>

Open in new window

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now