fork download
  1. <?php
  2.  
  3. $words = array('General', 'Director','President','General Manager','Constituency Aid',
  4. 'Data Input Clerk','Front Desk Clerk','General Director','Planning Officer');
  5. $urls = array('general', 'director','president', 'general-manager ', 'consistency-aid',
  6. 'data-input-clerk', 'front-desk-clerk', 'general-director', 'planning-officer');
  7. $srch = array_combine($words, $urls);
  8.  
  9. uksort($srch, function($a, $b) {
  10. if (strlen($a) == strlen($b))
  11. return 0;
  12. if (strlen($a) < strlen($b))
  13. return 1;
  14. return -1;
  15. });
  16.  
  17. $pattern= '/\b(?:' . implode("|", array_keys($srch)) . ')\b/i';
  18. $s = "Meet our new General Director!";
  19. echo preg_replace_callback($pattern, function($m) use ($srch) {
  20. return "<a href='" . $srch[$m[0]] . "'>" . $m[0] . "</a>";
  21. }, $s);
Success #stdin #stdout 0s 82944KB
stdin
Standard input is empty
stdout
Meet our new <a href='general-director'>General Director</a>!