Solved

Badwords filter, finding only "whole word"

Posted on 2011-03-07
8
485 Views
Last Modified: 2013-12-13
I have the following that works fine
But it finds also false badword within a longer word
Example:
I need to remove "cha"
it will rem it out of" any words cha and more"
but also in this form
"experts exchange" where if finds cha, which is indeed not OK
function unallowed($reply) {
	include($_SERVER['DOCUMENT_ROOT']."/badwords/bw.php");
	 
	$star = "*";
	
	for ($i = 0; $i < sizeof($badwords); $i++) {
		$censored = substr($badwords[$i], 0,1);
		for ($x = 1; $x < strlen($badwords[$i]); $x++) $censored .= $star;
		$reply = str_replace($badwords[$i], $censored, $reply);
	}
	return $reply;
}	

And
bw.php looks like this:
<?php
$badwords = 
array (
'zzzzz',
etc.....

Open in new window

0
Comment
Question by:array007
  • 4
  • 3
8 Comments
 
LVL 12

Expert Comment

by:Chad Haney
ID: 35060942
Use regex with preg_replace, if you want to censor the words out.  Or use preg_match to just get a true/false if the word is in the haystack.

http://php.net/manual/en/function.preg-replace.php
http://php.net/manual/en/function.preg-match.php
0
 
LVL 1

Author Comment

by:array007
ID: 35061174
Thanks
But regex is not my forte
could you show me the way with preg_replace?
regards
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 35061257
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

 
LVL 12

Expert Comment

by:Chad Haney
ID: 35061273
Like this.
<?php
$string = 'Experts Exchange.';
$pattern = '/cha/';
$replacement = '###';
echo preg_replace($pattern, $replacement, $string);

//Echos  Experts Ex###nge
?>

Open in new window

0
 
LVL 12

Expert Comment

by:Chad Haney
ID: 35061317
You can also do it as an array
<?php
$string = 'Experts Exchange has a lot of answers for questions.';
$patterns = array();
$patterns[0] = '/answers/';
$patterns[1] = '/for/';
$patterns[2] = '/questions/';
$replacements = array();
$replacements[0] = 'problems';
$replacements[1] = 'to';
$replacements[2] = 'solve';
echo preg_replace($patterns, $replacements, $string);
//Echos  Experts Exchange has a lot of problems to solve.
?>

Open in new window

0
 
LVL 1

Author Comment

by:array007
ID: 35061659
Thanks,
but it does not resolve my quest, the first solution is getting the same result as what I do know.
I am looking for it not to pick up "cha" in exchange but to pick it up in a str like: this is cha a str"
as a whole word only

And the other solution I am not up to adapt it
as I did not find a way to make my array working as the pattern
it looks like:
<?php
$badwords =
array (
'zzzzz',
etc.....
0
 
LVL 12

Accepted Solution

by:
Chad Haney earned 500 total points
ID: 35061803
Ah ok, I thought you were saying you wanted it to be replaced in any situation.  Try this.
<?php
print cleanWords("Experts Exchange cha.");

	function cleanWords($value) {

	/*   strip naughty words   */
	$badwords = array ('cha');
	
	for ($i = 0; $i < count($badwords); $i++) {
		$wordlist .= str_replace(chr(13),'',$badwords[$i]).'|';
	}
	$wordlist = substr($wordlist,0,-1);

	$value = preg_replace("/\b($wordlist)\b/ie", 'preg_replace("/./","*","\\1")', $value);	
	return $value;

}

?>

Open in new window

0
 
LVL 1

Author Closing Comment

by:array007
ID: 35062041
Thanks works a treat!
I just needed, as I got a var error,
to declare before the concat
wordlist as empty
$wordlist='';
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question