Supported Versions: PHP 4 >= 4.0.5, PHP 5, PHP 7, PHP 8
Perform a regular expression search and replace using a callback
<?php preg_replace_callback( string|array $pattern, callable $callback, string|array $subject, int $limit = -1, int &$count = null, int $flags = 0): string|array|null
<?php protected function formatKey($key)
{
return preg_replace_callback(
'/(.*)\.(.*)$/', fn ($matches) => sprintf(
'<fg=gray>%s ⇁</> %s',
str_replace('.', ' ⇁ ', $matches[1]),
$matches[2]
), $key
);
}
<?php {
$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($parameters, 0);
}, $path);
return trim(preg_replace('/\{.*?\?\}/', '', $path), '/');
}
<?php 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);
}
<?php 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);
}
}
<?php public static function replaceMatches($pattern, $replace, $subject, $limit = -1)
{
if ($replace instanceof Closure) {
return preg_replace_callback($pattern, $replace, $subject, $limit);
}
return preg_replace($pattern, $replace, $subject, $limit);
<?php 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));
<?php protected function storeVerbatimBlocks($value)
{
return preg_replace_callback('/(?<!@)@verbatim(\s*)(.*?)@endverbatim/s', function ($matches) {
return $matches[1].$this->storeRawBlock($matches[2]);
}, $value);
}
<?php protected function storePhpBlocks($value)
{
return preg_replace_callback('/(?<!@)@php(.*?)@endphp/s', function ($matches) {
return $this->storeRawBlock("<?php{$matches[1]}?>");
}, $value);
}
<?php protected function restoreRawContent($result)
{
$result = preg_replace_callback('/'.$this->getRawPlaceholder('(\d+)').'/', function ($matches) {
return $this->rawBlocks[$matches[1]];
}, $result);
$this->rawBlocks = [];
<?php >
return preg_replace_callback($pattern, function (array $matches) {
$this->boundAttributes = [];
$attributes = $this->getAttributesFromAttributeString($matches['attributes']);
return $this->componentString($matches[1], $attributes);
}, $value);
}
<?php \/>
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);
}
<?php >
$value = preg_replace_callback($pattern, function ($matches) {
$name = $this->stripQuotes($matches['inlineName'] ?: $matches['name'] ?: $matches['boundName']);
if (Str::contains($name, '-') && ! empty($matches['inlineName'])) {
$name = Str::camel($name);
}
if (! empty($matches['inlineName']) || ! empty($matches['name'])) {
$name = "'{$name}'";
}
$this->boundAttributes = [];
$attributes = $this->getAttributesFromAttributeString($matches['attributes']);
if (! empty($matches['inlineName']) && (! empty($matches['name']) || ! empty($matches['boundName']))) {
$attributes = ! empty($matches['name'])
? array_merge($attributes, $this->getAttributesFromAttributeString('name='.$matches['name']))
: array_merge($attributes, $this->getAttributesFromAttributeString(':name='.$matches['boundName']));
}
return " @slot({$name}, null, [".$this->attributesToString($attributes).']) ';
}, $value);
return preg_replace('/<\/\s*x[\-\:]slot[^>]*>/', ' @endslot', $value);
}
<?php {
$pattern = "/\s\:\\\$(\w+)/x";
return preg_replace_callback($pattern, function (array $matches) {
return " :{$matches[1]}=\"\${$matches[1]}\"";
}, $value);
}
<?php protected function parseComponentTagClassStatements(string $attributeString)
{
return preg_replace_callback(
'/@(class)(\( ( (?>[^()]+) | (?2) )* \))/x', function ($match) {
if ($match[1] === 'class') {
$match[2] = str_replace('"', "'", $match[2]);
return ":class=\"\Illuminate\Support\Arr::toCssClasses{$match[2]}\"";
}
return $match[0];
}, $attributeString
);
}
<?php protected function parseComponentTagStyleStatements(string $attributeString)
{
return preg_replace_callback(
'/@(style)(\( ( (?>[^()]+) | (?2) )* \))/x', function ($match) {
if ($match[1] === 'style') {
$match[2] = str_replace('"', "'", $match[2]);
return ":style=\"\Illuminate\Support\Arr::toCssStyles{$match[2]}\"";
}
return $match[0];
}, $attributeString
);
}