<?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;
- Output for 5.6.38, 7.1.0 - 7.1.33, 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.25, 8.4.1 - 8.4.12
- <p style="margin:20px auto; font-size:20px;"> <a style="color:red;" href="https://www.example.com/<a style="color:red;" href="https://www.example.com/happy1">happy</a>">hello</a>,this is a new <a style="color:red;" href="https://www.example.com/goodMood">function</a> ,Very <a style="color:red;" href="https://www.example.com/happy1">happy</a>! oh yes
<img src="https://example.com/foo.jpg" title="<a style="color:red;" href="https://www.example.com/happy1">happy</a>">
<a href="https://www.google.com" title="<a style="color:red;" href="https://www.example.com/happy1">happy</a>"></a>
</p>
preferences:
154.48 ms | 409 KiB | 5 Q