Solved

Badwords filter, finding only "whole word"

Posted on 2011-03-07
8
457 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
 
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

762 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now