public function replace($pattern, $replacement)
    {
        if (is_callable($replacement)) {
            $this->text = preg_replace_callback($pattern, function ($matches) use ($replacement) {
                $args = array_map(function ($item) {
                    return new Text($item);
                }, $matches);

                return call_user_func_array($replacement, $args);
            }, $this->text);
        } else {
            $this->text = preg_replace($pattern, $replacement, $this->text);
        }

        return $this;
    }
public function replace($pattern, $replacement)
    {
        if (is_callable($replacement)) {
            $this->text = preg_replace_callback($pattern, function ($matches) use ($replacement) {
                $args = array_map(function ($item) {
                    return new Text($item);
                }, $matches);

                return call_user_func_array($replacement, $args);
            }, $this->text);
        } else {
            $this->text = preg_replace($pattern, $replacement, $this->text);
        }

        return $this;
    }
public function replace ( $ pattern , $ replacement ) { if ( is_callable ( $ replacement ) ) { $ this -> text = preg_replace_callback ( $ pattern , function ( $ matches ) use ( $ replacement ) { $ args = array_map ( function ( $ item ) { return new Text ( $ item ) ; } , $ matches ) ; return call_user_func_array ( $ replacement , $ args ) ; } , $ this -> text ) ; } else { $ this -> text = preg_replace ( $ pattern , $ replacement , $ this -> text ) ; } return $ this ; }
Perform a regular expression search and replace

@param string          $pattern     The pattern to search for. It can be either a string or an array with strings.
@param string|callable $replacement The string or an array with strings to replace.
If $replacement is the callable, a callback that will be called and passed an array of matched elements in the subject string.

@return Text
Perform a regular expression search and replace