public static function replace(array $words, $replacement, $text)
    {
        $self = new self;

        $length = $self->wordsLength($words);

        $blocks = array_unique($length);

        $min = end($blocks);

        $chars = $self->splitText($text);

        $index = $self->index($chars);

        $pinyins = $self->pinyin->convert($text);

        $len = count($pinyins) - $min + 1;

        for ($i = 0; $i < $len; ++$i) {
            foreach ($blocks as $block) {
                if ($i + $block >= $len + $min) {
                    continue;
                }

                $hash = md5(implode(' ', array_slice($pinyins, $i, $block)));

                if (isset($length[$hash])) {
                    for ($c = 0; $c < $block; ++$c) {
                        $chars[$index[$i + $c]] = $replacement;
                    }

                    $i += $block - 1;

                    break;
                }
            }
        }

        return implode('', $chars);
    }
public static function replace(array $words, $replacement, $text)
    {
        $self = new self;

        $length = $self->wordsLength($words);

        $blocks = array_unique($length);

        $min = end($blocks);

        $chars = $self->splitText($text);

        $index = $self->index($chars);

        $pinyins = $self->pinyin->convert($text);

        $len = count($pinyins) - $min + 1;

        for ($i = 0; $i < $len; ++$i) {
            foreach ($blocks as $block) {
                if ($i + $block >= $len + $min) {
                    continue;
                }

                $hash = md5(implode(' ', array_slice($pinyins, $i, $block)));

                if (isset($length[$hash])) {
                    for ($c = 0; $c < $block; ++$c) {
                        $chars[$index[$i + $c]] = $replacement;
                    }

                    $i += $block - 1;

                    break;
                }
            }
        }

        return implode('', $chars);
    }
public static function replace ( array $ words , $ replacement , $ text ) { $ self = new self ; $ length = $ self -> wordsLength ( $ words ) ; $ blocks = array_unique ( $ length ) ; $ min = end ( $ blocks ) ; $ chars = $ self -> splitText ( $ text ) ; $ index = $ self -> index ( $ chars ) ; $ pinyins = $ self -> pinyin -> convert ( $ text ) ; $ len = count ( $ pinyins ) - $ min + 1 ; for ( $ i = 0 ; $ i < $ len ; ++ $ i ) { foreach ( $ blocks as $ block ) { if ( $ i + $ block >= $ len + $ min ) { continue ; } $ hash = md5 ( implode ( ' ' , array_slice ( $ pinyins , $ i , $ block ) ) ) ; if ( isset ( $ length [ $ hash ] ) ) { for ( $ c = 0 ; $ c < $ block ; ++ $ c ) { $ chars [ $ index [ $ i + $ c ] ] = $ replacement ; } $ i += $ block - 1 ; break ; } } } return implode ( '' , $ chars ) ; }
Replace words with replacements in a text.

@param array $words
@param string $replacement
@param string $text

@return string
Replace words with replacements in a text .