phppreg-replace

replace everything except specific words in PHP


Is it possible to use php's preg_replace to remove anything in a string except specific words?

For example:

$text = 'Hello, this is a              test string from php.';

I want to remove everything except "test" and "php" so it will be:

$text will be 'test php'

Solution

  • You could always use a callback. Under PHP 5.3:

    $keep = array('test'=>1, 'php'=>1);
    
    $text = trim(
        preg_replace(
            '/[^A-Za-z]+/', ' ',
            preg_replace_callback(
                '/[A-Za-z]+/',
                function ($matched) use (&keep) {
                    if (isset($keep[$matched[0]])) {
                        return $matched[0];
                    }
                    return '';
                }, $text
                )   )            );
    

    Alternatively:

    $text = 
        array_intersect(
            preg_split('/[^A-Za-z]+/', $text),
            array('test', 'php')
        );