How can I replace multiple words without using STR_REPLACE?

Asked

Viewed 877 times

2

For example, I want to take bad words out of a text.

How can I do using the substitution method ? Something that is faster.

Use the str_replace() is not very fast.

  • 2

    What do you mean it’s not fast? It would be nice to show how you’re doing, speed problem can be in the way you apply. o str_replace accepted arrays including, can make several substitutions in a single line. I gave example here, including the str_ireplace: http://answall.com/questions/35562/str-replace-usando-arrays

  • You can use the preg_replace() PREG_REPLACE In terms of performance I do not know if it makes much difference, but it is a good alternative, creates the Pattern and applies on the text, everything depends on the application.

  • I can use array on str_replace() ?

  • @Zoom has an example here http://answall.com/questions/35562/str-replace-usando-arrays

  • @Zoom yes, the 1st and 2nd parameters can be array.

  • In Preg_replace you can use arrays...

  • Taking that same idea, I can do highlights for searched words ?

  • 1

    @Fernandoa.W. but he wants something faster, not slower.

  • @zoom str_replace ( array( 'zoom', 'SOpt') , array( '<b>zoom</b>' , '<b>SOpt</b>' ), $texto ) so it will trade everything that is zoom by zoom and all that is Sopt by Sopt.

  • But that’s where the problem lies. To use array in the str_replace, both the from like the to has to be array. Isn’t it ? The text has to be converted every word into array.

  • @Zoom no, text is string. Array are the original and replacement lists.

  • Okay. I get it, I create a array with the list of ugly words and another for words I want to replace them or ####.

  • str_replace ( array( 'zoom', 'SOpt') , array( '<b>zoom</b>' , '<b>SOpt</b>' ), 'Eu sou o zoom do SOpt' ) will turn Eu sou o <b>zoom</b> do <b>SOpt</b>

  • @Zoom in this case, you can put the ugly ones as an array, and on the other side just '####' (so they all become the same thing). Then Voce uses array in search, and string in substitute.

  • All right. I’ll do it.

Show 10 more comments

1 answer

1


Can use preg_replace_callback() he’s fast.

In If you want to replace bad words with '*' here is a good example:

$badwords = array('bad1', 'bad2', 'bad3', 'ass');
$text = 'This is a test. Ass. Grass. bad1.';

function filterBadwords($text, array $badwords, $replaceChar = '*') {
    return preg_replace_callback(
        array_map(function($w) { return '/\b' . preg_quote($w, '/') . '\b/i'; }, $badwords),
        function($match) use ($replaceChar) { return str_repeat($replaceChar, strlen($match[0])); },
        $text
    );
}

echo filterBadwords($text, $badwords);

echo will print:

This is a test. ***. Grass. ****.

If on the other hand instead of replacing you want to highlight words you can use this function:

$badwords = array('bad1', 'bad2', 'bad3', 'ass');
$text = 'This is a test. Ass. Grass. bad1.';

function badwords_filter($string,$badwords){
        $p = implode('|', array_map('preg_quote', $words ));
        $string = preg_replace(
            '/('.$p.')/i', 
            '<span style="background:#fe5723; color:#fff">$1</span>', 
            $string
        );   
    return $string; 
}

echo badwords_filter($text, $badwords);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.