preg_replace_callback

Supported Versions: PHP 4 >= 4.0.5, PHP 5, PHP 7, PHP 8
Perform a regular expression search and replace using a callback
preg_replace_callback(    string|array $pattern,    callable $callback,    string|array $subject,    int $limit = -1,    int &$count null,    int $flags 0): string|array|null
    {

        
$path $this->replaceNamedParameters($path$parameters);



        
$path preg_replace_callback('/\{.*?\}/', function ($match) use (&$parameters) {


            
$parameters array_merge($parameters);



            return (! isset(
$parameters[0]) && ! str_ends_with($match[0], '?}'))

                        ? 
$match[0]

                        : 
Arr::pull($parameters0);

        }, 
$path);



        return 
trim(preg_replace('/\{.*?\?\}/'''$path), '/');

    }
    protected function replaceNamedParameters($path, &$parameters)

    {

        return 
preg_replace_callback('/\{(.*?)(\?)?\}/', function ($m) use (&$parameters) {

            if (isset(
$parameters[$m[1]]) && $parameters[$m[1]] !== '') {

                return 
Arr::pull($parameters$m[1]);

            } elseif (isset(
$this->defaultParameters[$m[1]])) {

                return 
$this->defaultParameters[$m[1]];

            } elseif (isset(
$parameters[$m[1]])) {

                
Arr::pull($parameters$m[1]);

            }



            return 
$m[0];

        }, 
$path);

    }
    function preg_replace_array($pattern, array $replacements$subject)

    {

        return 
preg_replace_callback($pattern, function () use (&$replacements) {

            foreach (
$replacements as $value) {

                return 
array_shift($replacements);

            }

        }, 
$subject);

    }

}
    public function replaceMatches($pattern$replace$limit = -1)

    {

        if (
$replace instanceof Closure) {

            return new static(
preg_replace_callback($pattern$replace$this->value$limit));

        }



        return new static(
preg_replace($pattern$replace$this->value$limit));
    protected function storeVerbatimBlocks($value)

    {

        return 
preg_replace_callback('/(?<!@)@verbatim(.*?)@endverbatim/s', function ($matches) {

            return 
$this->storeRawBlock($matches[1]);

        }, 
$value);

    }
    protected function storePhpBlocks($value)

    {

        return 
preg_replace_callback('/(?<!@)@php(.*?)@endphp/s', function ($matches) {

            return 
$this->storeRawBlock("<?php{$matches[1]}?>");

        }, 
$value);

    }
    protected function restoreRawContent($result)

    {

        
$result preg_replace_callback('/'.$this->getRawPlaceholder('(\d+)').'/', function ($matches) {

            return 
$this->rawBlocks[$matches[1]];

        }, 
$result);



        
$this->rawBlocks = [];
    protected function compileStatements($value)

    {

        return 
preg_replace_callback(

            
'/\B@(@?\w+(?:::\w+)?)([ \t]*)(\( ( (?>[^()]+) | (?3) )* \))?/x', function ($match) {

                return 
$this->compileStatement($match);

            }, 
$value

        
);

    }
            >




        return 
preg_replace_callback($pattern, function (array $matches) {

            
$this->boundAttributes = [];



            
$attributes $this->getAttributesFromAttributeString($matches['attributes']);



            return 
$this->componentString($matches[1], $attributes);

        }, 
$value);

    }
            \/>




        return 
preg_replace_callback($pattern, function (array $matches) {

            
$this->boundAttributes = [];



            
$attributes $this->getAttributesFromAttributeString($matches['attributes']);



            return 
$this->componentString($matches[1], $attributes)."\n@endComponentClass##END-COMPONENT-CLASS##";

        }, 
$value);

    }
            >




        
$value preg_replace_callback($pattern, function ($matches) {

            
$name $this->stripQuotes($matches['inlineName'] ?: $matches['name']);



            if (
Str::contains($name'-') && ! empty($matches['inlineName'])) {

                
$name Str::camel($name);

            }



            if (
$matches[2] !== ':') {

                
$name "'{$name}'";

            }



            
$this->boundAttributes = [];



            
$attributes $this->getAttributesFromAttributeString($matches['attributes']);



            return 
" @slot({$name}, null, [".$this->attributesToString($attributes).']) ';

        }, 
$value);



        return 
preg_replace('/<\/\s*x[\-\:]slot[^>]*>/'' @endslot'$value);

    }
                "<?php echo {$this->wrapInEchoHandler($matches[2])}; ?>{$whitespace}";

        };



        return 
preg_replace_callback($pattern$callback$value);

    }
            return $matches[1] ? substr($matches[0], 1) : "<?php echo {$wrapped}; ?>{$whitespace}";

        };



        return 
preg_replace_callback($pattern$callback$value);

    }
                "<?php echo e({$this->wrapInEchoHandler($matches[2])}); ?>{$whitespace}";

        };



        return 
preg_replace_callback($pattern$callback$value);

    }