Surligner des mots sans se fouler
By Geoffrey on Tuesday 23 May 2006, 18:22 - Coding - Permalink
/** * Hightlight words in a string * * @param string $haystack The string to highlight words in * @param array $needles The array containing the words to highlight * @param string $format The format to use in preg_replace * * highlight_words will return $haystack unmodified if $needles is empty * or if $format is invalid (eg: there is no "\1", which would cause the * preg_replace call to fail) */ function highlight_words($haystack, $needles, $format = '<strong class="highlight">\1</strong>') { if (empty($needles) || (strstr($format, '\1') == false)) { return $haystack; } return preg_replace('/('.implode('|', $needles).')/i', $format, $haystack); }
(Oui, ça aurait pu être aussi Poster sur son blog sans se fouler le cerveau)
EDIT: la voila la phpdoc :p
Comments
Hmmmm, mais il est tout pourri ce billet ....
ca te coutera un muffin
thks !
On pourrait aussi mettre une classe sur un span, histoire de pouvoir personnaliser le highlight :)
cam: putain batard t'attends que jsois parti pour me taxer mes muffins :(
Thanh: c'est le but de l'argument $format en fait :) mais c'est vrai qu'une classe par défaut ce serait pas superflux
Et elle est où la phpdoc ?
Ouah trop fort la màj ! Merci :p