Solved

Badwords filter, finding only "whole word"

Posted on 2011-03-07
8
476 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PHP search array for either partial or complete values 3 30
mysql between clause 2 25
Loop through multiple arrays 13 29
Fulfillment API php code sample 1 41
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

803 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