fork(1) download
  1. <?php
  2. $text = 'Я очень проголодался, не против бы пойти в столовую.';
  3. $words = [
  4. 'проголодался' => 'site1.com',
  5. 'пойти в столовую' => 'site2.com'
  6. ];
  7.  
  8. $patterns = array_map(function($v) {
  9. return '#(' . $v . '\b)#iu';
  10. }, array_keys($words));
  11.  
  12. $replacements = array_map(function($v) {
  13. return '<a href="' . $v . '">\1</a>';
  14. }, array_values($words));
  15.  
  16. $text = preg_replace($patterns, $replacements, $text);
  17.  
  18. echo $text;
Success #stdin #stdout 0.01s 24192KB
stdin
Standard input is empty
stdout
Я очень <a href="site1.com">проголодался</a>, не против бы <a href="site2.com">пойти в столовую</a>.