3v4l.org

run code in 300+ PHP versions simultaneously
<?php $str = "<p>Just a test: getName testName</p>"; $keywords = [ 0=>['id'=>1,'slug'=>'getName','url'=>'https://www.getname.com'], 1=>['id'=>2,'slug'=>'testName','url'=>'https://www.testname.com'], 2=>['id'=>3,'slug'=>'ign','url'=>'https://www.ign.com'] ]; $newKeywords = []; foreach ($keywords as $keyword) $newKeywords[$keyword['slug']] = $keyword['url']; $newStr = preg_replace_callback("/(\w+)/", function($m) use($newKeywords){ return isset($newKeywords[$m[0]]) ? "<a href='{$newKeywords[$m[0]]}'>{$m[0]}</a>" : $m[0]; }, $str); echo $newStr;

preferences:
23.7 ms | 404 KiB | 5 Q