Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-05-2012, 07:38 AM   PM User | #1
needsomehelp
Regular Coder

 
Join Date: Oct 2009
Posts: 302
Thanks: 4
Thanked 3 Times in 3 Posts
needsomehelp can only hope to improve
how to replace one phrase with another using arrays

I wish to filter inappropriate words using an array method.
I currently have...

$badwords = array("'word1'i", "'word2'i", "'word3'i"
);

That I have in a function that basically returns, good or bad, and if bad the page says so and stops the phrase being added until they change it.


So lets say someone enters in the field..
'I am a badword'
I wish to use an array method that will replace the badword with something else.

like so...


badword1 to goodword1
badword2 to goodword2

It would also need to be case insensitive.
needsomehelp is offline   Reply With Quote
Old 11-05-2012, 12:52 PM   PM User | #2
Redcoder
Regular Coder

 
Redcoder's Avatar
 
Join Date: May 2012
Location: /dev/couch
Posts: 309
Thanks: 2
Thanked 46 Times in 45 Posts
Redcoder has a little shameless behaviour in the past
There's no array function in PHP that does that - simply replacing a value in an array if a pre-defined string is found.

You'll just have to use a lo0p to do that for you. E.g a for loop

This is kinda crude but it's along the kines for what you should use

PHP Code:
for($i=0$i<=count($your_array))
{

if(
$your_array[$i] == 'string1' || $your_array[$i] == 'string1' || $your_array[$i] == 'string1'){

die(
"Bad word used');

}


__________________
For professional Hosting and Web design.....


NetEssentials.co.uk
Redcoder is offline   Reply With Quote
Old 11-05-2012, 01:28 PM   PM User | #3
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
You can use str_replace for this:
PHP Code:
$aBadWords = array('badword1''badword2');
$aGoodWords = array('goodword1''goodword2');

print 
str_replace($aBadWords$aGoodWords$input); 
str_ireplace can be done case insensitively. If you need more complex checks such as containing a phrase, then a pcre with preg_replace would be easier.
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
needsomehelp (11-05-2012)
Old 11-05-2012, 07:01 PM   PM User | #4
needsomehelp
Regular Coder

 
Join Date: Oct 2009
Posts: 302
Thanks: 4
Thanked 3 Times in 3 Posts
needsomehelp can only hope to improve
This one looks like it will do the job I need it for, thanks.

I had been racking my brain for a way to use an array that contains the badword and good work so the script would have checked for the first and replaced with the associated good word. but this method will work for me i hope.

thanks again.

Quote:
Originally Posted by Fou-Lu View Post
You can use str_replace for this:
PHP Code:
$aBadWords = array('badword1''badword2');
$aGoodWords = array('goodword1''goodword2');

print 
str_replace($aBadWords$aGoodWords$input); 
str_ireplace can be done case insensitively. If you need more complex checks such as containing a phrase, then a pcre with preg_replace would be easier.
needsomehelp is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:05 AM.


Advertisement
Log in to turn off these ads.