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 return $files->map(function($file) use ($pattern, $replacement) {
if($replacement instanceof Closure) {
$file['content'] = preg_replace_callback($pattern, $replacement, $file['content'], -1, $count);
} else {
$file['content'] = preg_replace($pattern, $replacement, $file['content'], -1, $count);
}
<?php static function replaceDynamicPlaceholders($event, $component)
{
return preg_replace_callback('/\{(.*)\}/U', function ($matches) use ($component) {
return data_get($component, $matches[1], function () use ($matches) {
throw new \Exception('Unable to evaluate dynamic event name placeholder: '.$matches[0]);
});
}, $event);
}
}
<?php static function replaceDynamicPlaceholders($key, $component)
{
return preg_replace_callback('/\{(.*)\}/U', function ($matches) use ($component) {
return data_get($component, $matches[1], function () use ($matches) {
throw new \Exception('Unable to evaluate dynamic session key placeholder: '.$matches[0]);
});
}, $key);
}
}
<?php {
$pattern = '/'.Regexes::$livewireOpeningTagOrSelfClosingTag.'/x';
return preg_replace_callback($pattern, function (array $matches) {
$attributes = $this->getAttributesFromAttributeString($matches['attributes']);
$keys = array_keys($attributes);
$values = array_values($attributes);
$attributesCount = count($attributes);
for ($i=0; $i < $attributesCount; $i++) {
if ($keys[$i] === ':' && $values[$i] === 'true') {
if (isset($values[$i + 1]) && $values[$i + 1] === 'true') {
$attributes[$keys[$i + 1]] = '$'.$keys[$i + 1];
unset($attributes[':']);
}
}
}
$attributes = collect($attributes)->mapWithKeys(function ($value, $key) {
if (str($key)->contains('_')) return [$key => $value];
return [(string) str($key)->camel() => $value];
})->toArray();
$attributes = collect($attributes)->mapWithKeys(function ($value, $key) {
if (! str($key)->contains('_')) return [$key => false];
return [(string) str($key)->camel() => $value];
})->filter()->merge($attributes)->toArray();
$component = $matches[1];
if ($component === 'styles') return '@livewireStyles';
if ($component === 'scripts') return '@livewireScripts';
if ($component === 'dynamic-component' || $component === 'is') {
if (! isset($attributes['component']) && ! isset($attributes['is'])) {
throw new ComponentAttributeMissingOnDynamicComponentException;
}
$component = $attributes['component'] ?? $attributes['is'];
unset($attributes['component'], $attributes['is']);
} else {
$component = "'{$component}'";
}
return $this->componentString($component, $attributes);
}, $value);
}
protected function componentString(string $component, array $attributes)
<?php public function livewire_only_precompilers_apply_to_livewire_components_and_not_normal_blade()
{
Livewire::precompiler(function ($string) {
return preg_replace_callback('/@foo/sm', function ($matches) {
return 'bar';
}, $string);
});
$output = Blade::render('
<?php $pattern = "/,\s*?key\(([\s\S]*)\)/"; // everything between ",key(" and ")"
$expression = preg_replace_callback($pattern, function ($match) use (&$key) {
$key = trim($match[1]) ?: $key;
return "";
}, $expression);
if (! $key) {
$key = app(\Livewire\Mechanisms\ExtendBlade\DeterministicBladeKeys::class)->generate();