preg_replace for preg_replace_callback

Asked

Viewed 264 times

4

How to make the function transition preg_replace to the preg_replace_callback in this case? I use arrays why in the future I can add continuation..

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
);
  • Please add method code $this->makelink and also the variable declaration $text.

  • I didn’t understand why you wanted to use this function, in this case, the regex pattern array, would not be for you to create a continuation of the link, but different link patterns.

1 answer

2

Perhaps you were confused as to the purpose of preg_replace_callback().

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

In your case it would be 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 class method you already have part of what preg_replace_callback() provides that it is the encapsulation and logical separation.

For what it’s worth, though, I would just remove those concatenations in the second array that don’t feel right.

Browser other questions tagged

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