Können Sie in preg_replace zwei Regex verwenden, um Elemente in einem Array abzugleichen und zu ersetzen? So zum Beispiel:Ersetzen von Elementen in einem Array mithilfe von zwei regulären Ausdrücken
Angenommen, Sie haben:
Array
(
[0] => mailto:[email protected]
[1] => mailto:[email protected]
[2] => mailto:[email protected]
[3] => mailto:[email protected]
[4] => mailto:[email protected]
[5] => mailto:[email protected]
[6] => mailto:[email protected]
}
und Sie haben zwei Variablen regulären Ausdrücke halten:
$reg = '/mailto:[\w-][email protected]([\w-]+\.)+[\w-]+/i';
$replace = '/[\w-][email protected]([\w-]+\.)+[\w-]+/i';
kann ich:
preg_replace($reg,$replace,$matches);
Um zu ersetzen "mailto: [email protected]" mit "[email protected]" in jedem Index des arra y.
die $ 1 in der Antwort (en) unten in das erste expand() Unterausdruck. Beachten Sie, dass wir (und) den Teil des Matches hinzugefügt haben, an dem Sie interessiert sind. – gahooa
Der dritte Parameter von preg_match() kann ein Array sein. In diesem Fall gibt preg_match() ein Array zurück. – gahooa