Solved

Badwords filter, finding only "whole word"

Posted on 2011-03-07
8
468 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

932 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

13 Experts available now in Live!

Get 1:1 Help Now