preg_replace no capture

advertisements

Trying to take in a large paragraph string and format a key word. My code is as follows:

function envisio($content){
    $env = '/(\s)+( (WORD|word|Word)(\s)?(AB|ab|Ab) )(\s)+/';
    $new = preg_replace ($env , '<span class="word">Word</span><span class="abb">AB</span>', $content);
    return $new;
}

I'm trying to catch all occurrences of "WordAB" (WORDAB, WORD AB, WordAB, Word AB etc.) and replace with the formatted html string. However, the function is not catching any of the occurances. Is it an error I am missing in my regex?


/word\s*ab/i case insensitive.

function envisio($content, $first, $second, $nowraps = array()) {

    $lookahead  = '';
    $lookbehind = '';

    if(count($nowraps) > 0) {
        $lookahead  = sprintf('(?![%s])', implode('', $nowraps));
        $lookbehind = sprintf('(?<![%s])', implode('', $nowraps));
    }

    $regex = sprintf('/%s%s\s?%s%s/i', $lookbehind, $first, $second, $lookahead);
    $replacement = sprintf('<span class="word">Word</span><span class="abb">AB</span>', $first, $second);
    return preg_replace($regex, $replacement, $content);
}

$str = '"WordAB" (WORDAB, WORD AB, WordAB, Word AB etc.) http://example.com/wordab/?a=wordab&b=WORDAB';

echo envisio($str, 'word', 'ab', array('\/', '\&', '?', '='));