3v4l.org

run code in 300+ PHP versions simultaneously
<?php $rows = [ ['href' => 'https://www.php.net/', 'name' => 'PHP.net'], ['href' => 'https://stackoverflow.com/', 'name' => 'Stack Overflow'], ['href' => 'https://www.google.com/', 'name' => 'Google'] ]; $rows_by_href = array_reduce($rows, function ($rows_by_href, $row) { $rows_by_href[$row['href']] = $row['name']; return $rows_by_href; }, []); $string = "<a href='https://www.php.net/'>https://www.php.net/</a> <a href='https://stackoverflow.com/'>https://stackoverflow.com/</a> <a href='https://www.google.com/'>https://www.google.com/</a>"; $pattern = "/<a href='(?<href>.+?)'>(?<name>.+?)<\/a>/i"; $result = preg_replace_callback($pattern, function ($matches) use ($rows_by_href) { return "<a href='" . $matches['href'] . "'>" . ($rows_by_href[$matches['href']] ?? $matches['name']) . "</a>"; }, $string); echo $result;
Output for 7.1.25 - 7.1.28, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.27, 8.4.1 - 8.4.14
<a href='https://www.php.net/'>PHP.net</a> <a href='https://stackoverflow.com/'>Stack Overflow</a> <a href='https://www.google.com/'>Google</a>

preferences:
144.52 ms | 408 KiB | 5 Q