preg_replace for preg_replace_callback

4

How do you transition the function preg_replace to preg_replace_callback in this case? I use arrays because I can add continuation in the future.

preg_replace(

    array(
        '/(?i)\b((?:https?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s'!()\[\]{};:\'".,<>?´ªìîëí]))/'
    ),

    array( '' . $this -> makelink( $1 ) . '' ), $text
);
    
asked by anonymous 30.06.2014 / 22:59

1 answer

2

You may have been confused about the purpose of preg_replace_callback () .

preg_replace_callback () mainly serves to make complex regular replacements more maintainable and compatible with future versions of PHP, by dispensing with occasional use of the PCRE Modifier and , encapsulating all the logic in a Closure .

In your case it would look something like this:

$line = preg_replace_callback(

    'sua_er',

    function( $matches ) {

        // Faz alguma coisa com $matches[ 0 ] e retorna
    },

    $line
);

However, makelink is already a method of a class you already have part of what preg_replace_callback () provides which is the logical encapsulation and separation.

If it's worth the caveat, though, I'd just remove those concatenations in the second array that do not seem right to me.

    
09.07.2014 / 19:47