src/Gobie/Regex/Wrappers/Pcre/PcreRegex.php
public static function replace($pattern, $replacement, $subject, $limit = -1)
    {
        static::setUp($pattern);

        if ((\is_object($replacement) || \is_array($replacement)) && \is_callable($replacement)) {
            foreach ((array) $pattern as $patternPart) {
                \preg_match($patternPart, '');
            }

            static::tearDown();

            $res = \preg_replace_callback($pattern, $replacement, $subject, $limit);

            if ($res === null) {
                static::handleError($pattern);
            }

            return $res;
        }

        $res = \preg_replace($pattern, $replacement, $subject, $limit);

        static::tearDown();
        static::handleError($pattern);

        return $res;
    }
public static function replace($pattern, $replacement, $subject, $limit = -1)
    {
        static::setUp($pattern);

        if ((\is_object($replacement) || \is_array($replacement)) && \is_callable($replacement)) {
            foreach ((array) $pattern as $patternPart) {
                \preg_match($patternPart, '');
            }

            static::tearDown();

            $res = \preg_replace_callback($pattern, $replacement, $subject, $limit);

            if ($res === null) {
                static::handleError($pattern);
            }

            return $res;
        }

        $res = \preg_replace($pattern, $replacement, $subject, $limit);

        static::tearDown();
        static::handleError($pattern);

        return $res;
    }
Regular expression replace and return replaced.

@param string|string[]          $pattern     Pattern or array of patterns
@param callable|string|string[] $replacement Replacement (string or callable) or array of string replacements
@param string|string[]          $subject     Subject or array of subjects
@param int                      $limit       Limit of replacements
@return string|string[] Replaced subject or array of subjects
@throws PcreRegexException When compilation or runtime error occurs
@link http://php.net/function.preg-replace.php
@link http://php.net/function.preg-replace-callback.php