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

$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

Hi,

have you considered preg_replace()? Basic example:

<?php

    $s = 'google googler';
    echo preg_replace('/\bgoogle\b/i', 'go*gle', $s);

    # outputs: go*gle googler

With arrays it gets tricky because you have to write the pattern for each word to filter, but you can use an anonymous function to fix it, for example:

<?php

    $words = array(
        'google',
        'googler',
        'yahoo',
        'yahoos',
        );

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

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

    $patterns = array_map(function($value) {
        return "/\b$value\b/i";
    }, $blacklist);

    $results = preg_replace($patterns, $replace, $words);
    print_r($results);

Returns:

Array
(
    [0] => go*gle
    [1] => googler
    [2] => yah*o
    [3] => yahoos
)

The anonymous function will create an array of patterns that looks like this:

Array
(
    [0] => /\bgoogle\b/i
    [1] => /\byahoo\b/i
)

The slashes / are delimiters. The \b is an escape character used to match the word boundaries, so the pattern used by preg_replace means: search only this specific word. The i is a modifier to use case insensitive search.

Docs
commented: This is how to answer a question +15

thank you for replying, already tried like this, is this already correct code?

$words = array(
        'google',
        'googler',
        'yahoo',
        'yahoos',
        );
    $replace = array(
        'go*gle',
        'yah*o'
        );
    $blacklist = array(
        'google',
        'yahoo'
        );
    $patterns = array_map(function($value) {
        return "/\b$value\b/i";
    }, $blacklist);

    $this->post['message'] = preg_replace($patterns, $replace, $words);
    print_r($this->post['message']);  

but its still not working & get error like this

Parse error: syntax error, unexpected T_FUNCTION, expecting ')' in /home/mobelof/public_html/file.php(296) : eval()'d code on line 25

Which PHP version are you using? If below 5.3 then you cannot use anonymous functions, just replace $patterns with this:

function patterns($value)
{
    return "/\b$value\b/i";
}

$patterns = array_map('patterns', $blacklist);

In practice array_map() is looping the blacklist array. Besides, the code you posted stops at line 20, the error is at line 25. So if the above doesn't solve, share the rest of the code. Bye!

old versions of php

'google ','yahoo ', //there's a space before the closing quote

'gogle ','yaho ',

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.