Jump to content

Recommended Posts

Hi.

 

I need some help with a bad word filter.

 

I have a variable to store the words in, seperated by comma.

Like this:

$badwords = "bad1,bad2,bad3,bad4";

 

Then I can use the explode(",", $badwords);

But, I now i don't know what to do.

 

I want to cut the away all letters but one, and replacing those letters with ** and keep the length of the word.

 

Like bad1 would be like this: b***

 

Anybody here that can help me?

Link to comment
https://forums.phpfreaks.com/topic/97626-bad-words-filter-help/
Share on other sites

Something like:

<?php
$badwords = array("bad1","bad2","bad3");

$post = "this is a bad1 word.";

foreach ($badwords as $key => $word)
{
$len = strlen($word) -1;
while ($len > 0)
	{
	$asterix .= "*";
	$len--;
	}
$post = str_ireplace($word,$word{0} . $asterix,$post);
}

echo $post;
?>

 

Untested.

Link to comment
https://forums.phpfreaks.com/topic/97626-bad-words-filter-help/#findComment-499516
Share on other sites

same concept...a little shorter:

 

<?php
$badwords = array("bad1","bad2","bad3");
$post = "this is a bad1 word.";
foreach ($badwords as $key => $word)
  $post = str_ireplace($word,$word{0}.str_repeat('*',strlen($word)-1),$post);
echo $post;
?>

Link to comment
https://forums.phpfreaks.com/topic/97626-bad-words-filter-help/#findComment-499525
Share on other sites

same concept...a little shorter:

 

<?php
$badwords = array("bad1","bad2","bad3");
$post = "this is a bad1 word.";
foreach ($badwords as $key => $word)
  $post = str_ireplace($word,$word{0}.str_repeat('*',strlen($word)-1),$post);
echo $post;
?>

ooo I didn't know there was a str_repeat function. Thanks.

Link to comment
https://forums.phpfreaks.com/topic/97626-bad-words-filter-help/#findComment-499529
Share on other sites

When i use the code i get this error:

 

Fatal error: Allowed memory size of 16777216 bytes exhausted (tried to allocate 16252905 bytes) in /customers/cordoproduction.com/cordoproduction.com/httpd.www/php/collection/Guestbook/guestbook.php on line 383

Which code are you talking about? The code in my box works, I made a change to it. You must have used the code a second before I corrected it.
Link to comment
https://forums.phpfreaks.com/topic/97626-bad-words-filter-help/#findComment-499536
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.