Facebook
From Kervan Mazuy, 1 Year ago, written in PHP.
Embed
Download Paste or View Raw
Hits: 180
  1. <?php
  2. $correspondances = array(
  3.   "mot1" => "https://www.lien1.com",
  4.   "mot2" => "https://www.lien2.com",
  5. );
  6.  
  7. function linkifyText($text, $correspondances) {
  8.   $pattern = "/#(\w+)#/";
  9.   $result = preg_replace_callback($pattern, function($matches) use ($correspondances) {
  10.     $word = $matches[1];
  11.     if (array_key_exists($word, $correspondances)) {
  12.       $link = '<a href="' . $correspondances[$word] . '">' . $word . '</a>';
  13.       return $link;
  14.     } else {
  15.       return $word;
  16.     }
  17.   }, $text);
  18.   return $result;
  19. }
  20.  
  21. $texte = "Voici un exemple de #mot1# et de #mot2#.";
  22. $texteLinkifie = linkifyText($texte, $correspondances);
  23. echo $texteLinkifie;
  24. ?&gt;