{"id":76,"date":"2009-05-06T11:54:19","date_gmt":"2009-05-06T09:54:19","guid":{"rendered":"http:\/\/luca.lovalvo.net\/1_luca_lo_valvo\/archive\/2180_dtecter__trouver_une_liste_de_mots_interdits_bani_dans_une_chaine_de_caractres_en_php.html"},"modified":"2022-11-04T15:57:43","modified_gmt":"2022-11-04T14:57:43","slug":"detecter-trouver-une-liste-de-mots-interdits-bani-dans-une-chaine-de-caracteres-en-php","status":"publish","type":"post","link":"https:\/\/luca.lovalvo.ch\/blog\/detecter-trouver-une-liste-de-mots-interdits-bani-dans-une-chaine-de-caracteres-en-php\/","title":{"rendered":"D\u00e9tecter \/ trouver une liste de mots interdits (bani) dans une chaine (de caract\u00e8res) en php"},"content":{"rendered":"<p>Petite fonction &laquo; vite faite &raquo; permettant de d&eacute;tecter dans une cha&icirc;ne de caract&egrave;res, si un ou des mots bannis y figurent.<\/p>\n<p>Dans la pratique, cela servirait en bloquer une action en cas de mots injurieux.<\/p>\n<p><strong>La fonction: <\/strong><\/p>\n<p>&lt;?php<br \/>function IsBLWord($string)<br \/>&nbsp;&nbsp; &nbsp;{<br \/><font color=\"#339966\">&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;\/\/LISTE DES MOTS A TROUVER<br \/>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;$badword=array(<strong>&quot;CONNARD&quot;,&quot;ENCULE&quot;<\/strong>);<\/font><\/p>\n<p>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;$i = 0; \/\/initialisation du compteur<br \/>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;$Cbadword=count($badword);<br \/>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;while ($i &lt;= $Cbadword-1) {<br \/>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if (preg_match(&quot;\/b$badword[$i]b\/i&quot;, $string)) <br \/>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;{<br \/>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;$i=$Cbadword; \/\/un des mots a &eacute;t&eacute; trouv&eacute;, on stop la boucle<br \/>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;$finded=&quot;1&quot;;&nbsp; \/\/on indique qu&#39;un mot a &eacute;t&eacute; trouv&eacute;<br \/>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;} <br \/>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;else <br \/>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;{<br \/>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;$finded=&quot;0&quot;;&nbsp;&nbsp; &nbsp;\/\/on indique qu&#39;aucun mot a &eacute;t&eacute; trouv&eacute;<br \/>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}<br \/>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;<br \/>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;$i++;<br \/>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}<\/p>\n<p>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if($finded==&quot;1&quot;)<br \/>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;{RETURN true;} \/\/on mot &eacute;t&eacute; trouv&eacute;, on retourne true<br \/>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;else<br \/>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;{RETURN false;} \/\/aucun mot a &eacute;t&eacute; trouv&eacute;, on retourne false<br \/>&nbsp;&nbsp; &nbsp;}<br \/>?&gt;<\/p>\n<p><strong>Utilisation :<\/strong><\/p>\n<p>&lt;?php<br \/>$txt=&quot;Le voila le <font color=\"#008000\">connard<\/font> du quartier&quot;;<\/p>\n<p>if(<font color=\"#008000\">IsBLWord($txt)==TRUE<\/font>)<br \/>&nbsp;&nbsp;&nbsp;&nbsp; {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; <font color=\"#3366ff\">exit;<\/font> \/\/on bloque l&#39;action car le terme &quot;<font color=\"#008000\">connard<\/font>&quot; figure dans la chaine. <\/p>\n<p> &nbsp;&nbsp;&nbsp; }<\/p>\n<p>&nbsp;<\/p>\n<p>?&gt; <\/p>\n<p>&nbsp;<\/p>\n<p>Il y a certainement moyen d&#39;optimiser cela&#8230; ou utiliser une autre regex.<br \/>mais celle-ci fonctionne tr&egrave;s bien pour mon utilisation \ud83d\ude42<\/p>\n<p><font color=\"#ff0000\">UPDATE : La fonction PHP <a href=\"http:\/\/ch2.php.net\/manual\/fr\/function.strpos.php\" target=\"_blank\" rel=\"noopener\">strpos()<\/a> peut &ecirc;tre utilis&eacute;e si l&#39;on veut d&eacute;tecter qu&#39;un mot. <\/font><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Petite fonction &laquo; vite faite &raquo; permettant de d&eacute;tecter dans une cha&icirc;ne de caract&egrave;res, si un ou des mots bannis y figurent. Dans la pratique, cela servirait en bloquer une action en cas de mots injurieux. La fonction: &lt;?phpfunction IsBLWord($string)&nbsp;&nbsp; &nbsp;{&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;\/\/LISTE DES MOTS A TROUVER&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;$badword=array(&quot;CONNARD&quot;,&quot;ENCULE&quot;); &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;$i = 0; \/\/initialisation [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-76","post","type-post","status-publish","format-standard","hentry","category-programmation"],"_links":{"self":[{"href":"https:\/\/luca.lovalvo.ch\/blog\/wp-json\/wp\/v2\/posts\/76","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/luca.lovalvo.ch\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/luca.lovalvo.ch\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/luca.lovalvo.ch\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/luca.lovalvo.ch\/blog\/wp-json\/wp\/v2\/comments?post=76"}],"version-history":[{"count":1,"href":"https:\/\/luca.lovalvo.ch\/blog\/wp-json\/wp\/v2\/posts\/76\/revisions"}],"predecessor-version":[{"id":284,"href":"https:\/\/luca.lovalvo.ch\/blog\/wp-json\/wp\/v2\/posts\/76\/revisions\/284"}],"wp:attachment":[{"href":"https:\/\/luca.lovalvo.ch\/blog\/wp-json\/wp\/v2\/media?parent=76"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/luca.lovalvo.ch\/blog\/wp-json\/wp\/v2\/categories?post=76"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/luca.lovalvo.ch\/blog\/wp-json\/wp\/v2\/tags?post=76"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}