<?php
$body = '
<p style="margin:20px auto; font-size:20px;"> hello,this is a new function ,Very happy! oh yes
<img src="https://example.com/foo.jpg" title="happy">
<a href="https://www.google.com" title="happy"></a>
</p>';
$str = htmlspecialchars_decode($body);
$keywords = [
0=>['id'=>1,'title'=>'function','url'=>'https://www.example.com/goodMood'],
1=>['id'=>2,'title'=>'hello','url'=>'https://www.example.com/happy'],
2=>['id'=>3,'title'=>'happy','url'=>'https://www.example.com/happy1'],
];
$content = array_reduce($keywords, function ($c, $v) {
return preg_replace('/(' . $v['title'] . ')/', '<a style="color:red;" href="'. $v['url'] . '">$1</a>', $c);
}, $str);
echo $content;
preferences:
24.31 ms | 406 KiB | 5 Q