Jump to content

Recommended Posts

I have the bad word censoring function code below:

 

function censorWords($search_words, $replacement , $message)
{
	/*
	Usage:

	$searchwords = array("hat","dog","cat"); // Censored words

	$message = "The dog had a hat the cat was jealous of hatting.";

	$replacement = "*";

	echo ($page->censorWords($searchwords, $replacement, $message));
	*/

	foreach ($search_words as $search_word)
	{
		$length = strlen($search_word);

		$stringreplacement = "";

		for( $i = 0; $i < $length; $i++ )
		{
			$stringreplacement .= $replacement;
		}

		$message = eregi_replace("($search_word)", $stringreplacement , $message);
	}
	return $message;
}

 

I have not included any actual curse words in this example.

 

This function is almost working but when I run it using the usage data that I've given it returns:

 

The *** had a *** the *** was jealous of ***ting.

 

How can I make it censor the rest of a censored word?

 

e.g. output:

 

The *** had a *** the *** was jealous of ******.

 

instead?

Link to comment
https://forums.phpfreaks.com/topic/116583-php-langauge-censoring-function/
Share on other sites

You require a more complex regex specifying word boudaries.

The best tool I have found as regex can be complex is regex buddy. Check it out: http://www.regexbuddy.com/

 

This tool you will use more than once for sure and is definately worth getting even if the regex for your query is posted by another user.

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.