a1amattyj Posted January 23, 2009 Share Posted January 23, 2009 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! Quote Link to comment Share on other sites More sharing options...
premiso Posted January 23, 2009 Share Posted January 23, 2009 $words = explode("\\n", $SystemSettings['censored_words']); Shoudd be: $words = explode("\n", $SystemSettings['censored_words']); You do not need to escape the \ as the \n is it's own character. And use str_replace over ereg_replace for simple replacements like you have. Quote Link to comment Share on other sites More sharing options...
premiso Posted January 23, 2009 Share Posted January 23, 2009 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. Quote Link to comment Share on other sites More sharing options...
a1amattyj Posted January 23, 2009 Author Share Posted January 23, 2009 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. Quote Link to comment Share on other sites More sharing options...
premiso Posted January 23, 2009 Share Posted January 23, 2009 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? Quote Link to comment Share on other sites More sharing options...
a1amattyj Posted January 23, 2009 Author Share Posted January 23, 2009 String in > "here is a example ." echo'ed values from $SystemSettings['censored_words'] are badword example2 example testing Thanks! Quote Link to comment Share on other sites More sharing options...
premiso Posted January 23, 2009 Share Posted January 23, 2009 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. Quote Link to comment Share on other sites More sharing options...
printf Posted January 23, 2009 Share Posted January 23, 2009 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! Quote Link to comment Share on other sites More sharing options...
a1amattyj Posted January 23, 2009 Author Share Posted January 23, 2009 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! Quote Link to comment Share on other sites More sharing options...
premiso Posted January 23, 2009 Share Posted January 23, 2009 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. Quote Link to comment Share on other sites More sharing options...
a1amattyj Posted January 23, 2009 Author Share Posted January 23, 2009 Thanks, separated with comma, thanks for your time Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.