<?php $str = ' I love [123] and [124] at (456). I hate [234] and [235] at (123).'; $table1Col1=["123" => "a", "124" => "b", "234" => "c", "235" => "d"]; $table1Col2 = ["456" => "XX", "123" => "ZZ"]; $pattern = "/\[(\d+)]|\((\d+)\)/"; $result = preg_replace_callback($pattern, function($match) use ($table1Col1, $table1Col2) { return sprintf('<a title="%s">%s</a>', array_key_exists(2, $match) ? $table1Col2[$match[2]] : $table1Col1[$match[1]] , $match[0] ); }, $str); echo $result;
You have javascript disabled. You will not be able to edit any code.