Jump to content

[SOLVED] foreach replacing bad words


a1amattyj

Recommended Posts

Hello,

 

Function to replace all bad words in a exploded string. Separated with a new line for each word.

 

function badWords($clean)
{
global $SystemSettings;

if(!empty($SystemSettings['censored_words']))
{
	$words = explode("\\n", $SystemSettings['censored_words']);

	foreach ($words as  $value)
	{

		$clean = eregi_replace($value,$SystemSettings['filtered_outcome'],$clean);

	}


}


return $clean;

}

 

Problem is, its not replacing any of the bad words, it does nothing.

 

Trying echo'ijng out each $value, does so correctly. So i assume its the eregi_replace im going wrong with?

 

Thanks!

Link to comment
Share on other sites

And thinking about it:

function badWords($clean)
{
   global $SystemSettings;
   
   if(!empty($SystemSettings['censored_words'])) {
      $words = explode("\n", $SystemSettings['censored_words']);
      $clean = str_replace($words, $SystemSettings['filtered_outcome'], $clean);  
   }
      
   return $clean;
}

 

Should work without the foreach.

Link to comment
Share on other sites

Hi,

 

Thanks for your help!

 

However, both of those suggestions don't seem to be working. It just returns the same as what it came in as.

 

 

Are you sure that censored_words is actually being populated right? Can you post a sample of an echo'd out version of that?

Link to comment
Share on other sites

I do not see them being split by a newline, just by a space. If that is correct, this should work:

 

function badWords($clean)
{
   global $SystemSettings;
   
   if(!empty($SystemSettings['censored_words'])) {
      $words = explode(" ", $SystemSettings['censored_words']);
      $clean = str_replace($words, $SystemSettings['filtered_outcome'], $clean);  
   }
      
   return $clean;
}

 

If that still does not work, lets try this for kicks

 

 

function badWords($clean)
{
   global $SystemSettings;
   
   if(!empty($SystemSettings['censored_words'])) {
      $words = array("word1", "word2");
      $clean = str_replace($words, $SystemSettings['filtered_outcome'], $clean);  
   }
      
   return $clean;
}

 

And if you are using php 5 or greater use str_ireplace instead. So it is a case-insensitive search.

Link to comment
Share on other sites

use $words = array_map ( 'trim', explode ( "\n", $SystemSettings['censored_words'] ) ); just in case you might be running this on different systems! Perl by default converts \r\n & \r to \n no matter what system it runs on, PHP does not, so if your using windows you need to use \r\n on Linux \n and on Mac \r. If on windows and you don't do that your _replace function will be looking to replace badword\r which is not what you want!

Link to comment
Share on other sites

Hi,

 

The first code didn't work, however your second one did. Is it worth separating each word with a comma or something similar in the database to try?

 

Thanks!

 

I generally separate lists in my db using a , especially ones I know will not contain other commas, then explode at that. Makes it easier and you know what to explode it and know that it should work each time. But that is just me.

Link to comment
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.