Webmaster Forum

Go Back   Webmaster Forum > Web Development > Coding Forum

Coding Forum Problems with your code? Discuss coding issues, including JavaScript, PHP & MySQL, HTML & CSS, Flash & ActionScript, and more.


Reply
 
Thread Tools Display Modes
Share |
  #1  
Old 05-22-2015, 04:54 PM
basketmen basketmen is offline
Contributing Member
 
Join Date: 03-21-07
Posts: 97
iTrader: 0 / 0%
[PHP] Replacing exact words

Hi guys, below are simple replacing script a word in to another word, or censorship

PHP Code:
$word = array(
'google',
'yahoo'
);

$link = array(
'go*gle',
'yah*o'
);

$this->post['message'] = str_ireplace($word$link$this->post['message']); 

the problem its currently also replacing another words like
Quote:
googler
yahoos
into
Quote:
go*gler
yah*os
please help how to make it only replacing exact words?
GBU for all that answering
 
Reply With Quote

Advertisement

Advertisement

  #2  
Old 05-22-2015, 05:39 PM
HTMLBasicTutor's Avatar
HTMLBasicTutor HTMLBasicTutor is offline
Administrator
 
Join Date: 10-29-07
Location: Canada
Posts: 26,707
iTrader: 5 / 100%
I think needs to look for the absolute value of the words you are trying to replace.
 
Reply With Quote
  #3  
Old 05-23-2015, 05:06 AM
ScriptMan's Avatar
ScriptMan ScriptMan is offline
Super Moderator
 
Join Date: 02-10-07
Location: Central Kentucky
Posts: 14,016
iTrader: 4 / 100%
You are either using that incorrectly or in a format I've never seen.

This format does work. Sine it is a badwords list I have edited each word in the $bad to a single character some of which appear tp be duplicates because of the editing and this example only has one substitute word.

PHP Code:
// bad words array
        
$bad =  array('s','f''c''c''0','n''a''e''m''m''p''j''b''b',  ); 
      
        
$good='-BLEEP-'
        
$string str_ireplace($bad,$good,$comment);
        
$word=$good
 
Reply With Quote
  #4  
Old 05-23-2015, 03:16 PM
basketmen basketmen is offline
Contributing Member
 
Join Date: 03-21-07
Posts: 97
iTrader: 0 / 0%
Quote:
Originally Posted by HTMLBasicTutor View Post
I think needs to look for the absolute value of the words you are trying to replace.
thank you for the link

i am a little newbie, i already red it, but where to put it in the code?






Quote:
Originally Posted by ScriptMan View Post
You are either using that incorrectly or in a format I've never seen.

This format does work. Sine it is a badwords list I have edited each word in the $bad to a single character some of which appear tp be duplicates because of the editing and this example only has one substitute word.

PHP Code:
// bad words array
        
$bad =  array('s','f''c''c''0','n''a''e''m''m''p''j''b''b',  ); 
      
        
$good='-BLEEP-'
        
$string str_ireplace($bad,$good,$comment);
        
$word=$good
thank you for replying
where i need to put $this->post['message'] btw?
 
Reply With Quote
  #5  
Old 05-23-2015, 05:40 PM
J. H. Rasmussen J. H. Rasmussen is online now
Super Moderator
 
Join Date: 11-11-11
Location: Copenhagen, Denmark
Posts: 5,997
iTrader: 1 / 100%
Post

I think the right thing to do in this case, is to use regular expression.

Here is an example with regular expression you should be able to use by replacing it with the PHP-code you have posted:

PHP Code:
$word = array(
    
'google',
    
'yahoo'
);

$link = array(
    
'go*gle',
    
'yah*o'
); 

foreach(
$word as $key => $value)
{
    
$word[$key] = '/\b' $value '\b/i';
}

$this->post['message'] = preg_replace($word$link$this->post['message']); 
I have tested it, so it should work.

The foreach loop replaces the items in the array with regular expression to use with preg_replace. So for example 'google' becomes '/\bgoogle\b/i'. the i-wrapper '//i' makes the expression case insensitive, the '\b' before and after the word google make sure that the word isn't replaced if there is a word character before or after the word we are searching for in this case 'google'. So, 'Google' or 'Google.' will be replaced, but 'iGoogle' or 'Googler' will not be replaced.
 
Reply With Quote
  #6  
Old 05-24-2015, 12:03 PM
basketmen basketmen is offline
Contributing Member
 
Join Date: 03-21-07
Posts: 97
iTrader: 0 / 0%
Quote:
Originally Posted by J. H. Rasmussen View Post
I think the right thing to do in this case, is to use regular expression.

Here is an example with regular expression you should be able to use by replacing it with the PHP-code you have posted:

PHP Code:
$word = array(
    
'google',
    
'yahoo'
);

$link = array(
    
'go*gle',
    
'yah*o'
); 

foreach(
$word as $key => $value)
{
    
$word[$key] = '/\b' $value '\b/i';
}

$this->post['message'] = preg_replace($word$link$this->post['message']); 
I have tested it, so it should work.

The foreach loop replaces the items in the array with regular expression to use with preg_replace. So for example 'google' becomes '/\bgoogle\b/i'. the i-wrapper '//i' makes the expression case insensitive, the '\b' before and after the word google make sure that the word isn't replaced if there is a word character before or after the word we are searching for in this case 'google'. So, 'Google' or 'Google.' will be replaced, but 'iGoogle' or 'Googler' will not be replaced.
its works, thanks a lot, you really saved me

GBU for HTMLBasicTutor, ScriptMan and J. H. Rasmussen
 
Reply With Quote
Go Back   Webmaster Forum > Web Development > Coding Forum

Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
Google Replacing CEO? Joe Bestler Google Forum 6 01-21-2011 11:21 PM
Writing Articles ($2 Per 500 Words / $5 Per 1k Words / $10 Per 1.5k Words) KMRock Content 3 08-01-2009 09:00 AM


V7N Network
Get exposure! V7N I Love Photography V7N SEO Blog V7N Directory


All times are GMT -7. The time now is 12:08 AM.
Powered by vBulletin
Copyright 2000-2014 Jelsoft Enterprises Limited.
Copyright © 2003 - 2018 VIX-WomensForum LLC