0

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

Edited by basketmen

3
Contributors
4
Replies
31
Views
2 Years
Discussion Span
Last Post by almostbob
2

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

Edited by cereal

Votes + Comments
This is how to answer a question
0

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
0

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!

0

old versions of php

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

'gogle ','yaho ',

Edited by almostbob

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.