> What I'm trying to do: > > I'm trying to have the proper variables for the eregi_replace so that each > word in the $dico array is replaced by a link to its definition respecting > the following rules : > -only exact match makes a link to the definition, for example : > - "abaisseraient" doesn't make a link > - "reabaisser" doesn't make a link > - " abaisser," or "smthg.Abaisser " makes a link on "abaisser" only, > so that the whitespace, comma or dot isn't part of the link > - "abaisser" a the end of the string make a link > > -I don't want to make a link when already in a link : > - "<a href='some_page.php'>tu peux abaisser le levier</a>" doesn't not > make a link on "abaisser" > > I know I'm bad with regex and really need some help on this. This would be > really appreciated.
I gave it my best, I could not figure out how to do it in a single regex. You could make this code more efficient but I think this might do what you want. <? $dico = array( "abaisser" => "def.php?id=9", "singer" => "def.php?id=9" ); $texte = "Abaisser Vous abaisserez la pate, abaisser.Singer Abaisser Vous abaisserez la pate,<a href=\"some_page.php\"> abaisser.Singer</a>singer<a href=\"some_page_other.php\"> abaisser.Singer</a>"; echo $texte."<br />\n"; preg_match_all('/<a\s+.*?href=[\"\'\s]?.*?>.*?<\/a>/i', $texte, $matches); if (is_array($matches[0])) { $counter = 0; foreach ($matches[0] as $value){ $texte = str_replace ( $value, " REPLACEME$counter ", $texte); $counter++; } } foreach ($dico as $mot => $def) { $pattern = '/('.$mot.'\b)/i'; print "$pattern<br />\n"; $texte = preg_replace($pattern,"<a href=\"$def\">$1</a>",$texte); } if (is_array($matches[0])) { $counter = 0; foreach ($matches[0] as $value){ $texte = str_replace ( " REPLACEME$counter ", $value, $texte); $counter++; } } echo $texte; ?> -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php