Register Advertise with usHelp Desk Today's Posts Search

Closed Thread
Thread Tools Search this Thread
Unread 22nd May 2015, 05:52 PM   #1
Active Warrior
 
Join Date: 2010
Posts: 58
Thanks: 2
Thanked 1 Time in 1 Post
Default
[PHP] Replacing exact words
Share on: 
fb share twitter share gplus share more share

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
googler
yahoos
into
go*gler
yah*os

please help how to make it only replacing exact words?
GBU for all that answering

I like forum
basketmen is offline  
Unread 22nd May 2015, 10:51 PM   #2
Active Warrior
War Room Member
 
Join Date: 2013
Posts: 48
Thanks: 0
Thanked 9 Times in 9 Posts
Default
Re: [PHP] Replacing exact words
Share on: 
fb share twitter share gplus share more share

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

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

$this->post['message'] = str_ireplace($word, $link, $this->post['message']); 
softwarewarden is offline  
Unread 23rd May 2015, 03:52 AM   #3
Web Designer
 
Join Date: 2013
Posts: 70
Thanks: 0
Thanked 19 Times in 18 Posts
Default
Re: [PHP] Replacing exact words
Share on: 
fb share twitter share gplus share more share

PHP Code:
$message = 'google yahoo,  googler yahoos';

$word = array(
'google',
'yahoo'
);

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


$combine = array_combine($word, $link);

$callback = function($match) use ($combine){
    return 
$combine[$match[1]];
};

$exp = '/\b('. implode('|', $word).')\b/';

$message = preg_replace_callback($exp, $callback, $message);

echo 
$message; 
Nathan K is offline  
Unread 23rd May 2015, 03:32 PM   #4
Active Warrior
 
Join Date: 2010
Posts: 58
Thanks: 2
Thanked 1 Time in 1 Post
Default
Re: [PHP] Replacing exact words
Share on: 
fb share twitter share gplus share more share

thank you Nathan for replying, already tried like this, but its get error like this

PHP Code:
Parse error: syntax error, unexpected T_FUNCTION in /home/username/public_html/file.php(296) : eval()'d code on line 16 
any other i can try?

I like forum
basketmen is offline  
Unread 23rd May 2015, 03:36 PM   #5
Active Warrior
 
Join Date: 2010
Posts: 58
Thanks: 2
Thanked 1 Time in 1 Post
Default
Re: [PHP] Replacing exact words
Share on: 
fb share twitter share gplus share more share

thank you for replying softwarewarden, its works if the words in the middle of text, its simple way, but not works if the words in the front or back, like this

PHP Code:
googleg*oglegoogle
yahooyah
*oyahoo 
any other code please?

I like forum
basketmen is offline  
Unread 23rd May 2015, 04:12 PM   #6
Web Designer
 
Join Date: 2013
Posts: 70
Thanks: 0
Thanked 19 Times in 18 Posts
Default
Re: [PHP] Replacing exact words
Share on: 
fb share twitter share gplus share more share

The reason it did not work is because of your php version. try this
PHP Code:
$message = 'google yahoo,  googler googleyahoosgoogle';

$word = array(
'google',
'yahoo'
);

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

$combine = array_combine($word, $link);

$exp = '/\b('. implode('|', $word).')\b/';

$message = preg_replace_callback($exp, function($match) use ($combine){
    return 
$combine[$match[1]];
}, 
$message);

echo 
$message; 
I dont think this is possible

googleg*oglegoogle
yahooyah*oyahoo

Because your first requirement is not to replace words like "googler" and in your second requirement is replace words like "googler".

If you want to replace all the words remove the two '\b's in $exp.
Nathan K is offline  
Closed Thread


Bookmarks

Tags
exact, php, replacing, words


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
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT -6. The time now is 04:06 AM.