3v4l.org

run code in 300+ PHP versions simultaneously
<?php $s = 'Other text1 {r|url-slug1|LinkTitle1} Other text2 {t|url-slug2|LinkTitle2} Other text3'; $pattern = '~ (?<before> .*? ) { (?: r (*MARK:ref) | t (*MARK:type) ) \Q|\E (?<url_slug> [^|]+ ) \Q|\E (?<linkTitle> [^}]+ ) } | (?<after> .+ ) ~xs'; preg_match_all($pattern, $s, $matches, PREG_SET_ORDER); $res = array_reduce($matches, fn($c, $i) => $c . ( $i['after'] ?? $i['before'] . '<a href="/' . $i['MARK'] . '/' . $i['url_slug']. '">' . $i['linkTitle'] . '</a>' ) ); echo $res;

preferences:
102.83 ms | 1404 KiB | 5 Q