Code
<?php
declare(strict_types=1);
$text = 'Kontakt: anna@example.com, ben@site.de';
// Alle E-Mails extrahieren
if (preg_match_all('/[\w.+-]+@[\w.-]+\.[a-z]{2,}/i', $text, $matches)) {
foreach ($matches[0] as $email) {
echo $email . "\n";
}
}
// Mit Named Groups
if (preg_match('#(?<jahr>\d{4})-(?<monat>\d{2})-(?<tag>\d{2})#', '2026-09-18', $m)) {
echo "{$m['tag']}.{$m['monat']}.{$m['jahr']}";
}
// Ersetzen mit Callback
$masked = preg_replace_callback(
'/\b(\d{3})(\d+)\b/',
fn($m) => $m[1] . str_repeat('*', strlen($m[2])),
'Karte: 1234567890'
);
anna@example.com