{"id":1980,"date":"2014-01-20T17:07:26","date_gmt":"2014-01-20T16:07:26","guid":{"rendered":"http:\/\/www.manuelmarangoni.it\/sir-bit\/?p=1980"},"modified":"2014-01-28T11:15:37","modified_gmt":"2014-01-28T10:15:37","slug":"php-censura-o-sostituire-una-serie-di-parole-con-unaltra","status":"publish","type":"post","link":"https:\/\/www.manuelmarangoni.it\/sir-bit\/1980\/php-censura-o-sostituire-una-serie-di-parole-con-unaltra\/","title":{"rendered":"PHP &#8211; Censurare o sostituire una serie di parole con un&#8217;altra"},"content":{"rendered":"<p>Quando all&#8217;utente si lascia la possibilit\u00e0 di commentare articoli, di recensire prodotti o di scrivere le proprie opinioni, pu\u00f2 essere utile far uso di una <strong>funzione che censuri una serie di parole<\/strong> ritenute &#8220;scomode&#8221; o &#8220;di spam&#8221;.<\/p>\n<p>Con la funzione che andiamo a vedere, basta creare un array di parole: lo script si occuper\u00e0 di rintracciarle nel testo e di <strong>sostituirle con un&#8217;altra parola data<\/strong>.<br \/>\nSe la parola di sostituzione non \u00e8 stata passata, le stringhe da &#8220;eliminare&#8221; saranno semplicemente censurate da una serie di cancelletti (&#8220;#&#8221;).<\/p>\n<p>Lo script, completo di esempio, \u00e8 liberamente scaricabile dal link qui sotto.<br \/>\nNon necessita di modifiche, a meno che non la vogliate integrare con ulteriori controlli sui parametri (che per semplicit\u00e0 ho ridotto al minimo).<!--more--><\/p>\n<div style=\"float: left; width: 100%; margin-top: 60px; margin-bottom: 40px;\">\n<div style=\"float: left; width: 100px;\"><img decoding=\"async\" title=\"PHP - Censura o sostituire una serie di parole con un'altra\" alt=\"PHP - Censura o sostituire una serie di parole con un'altra\" src=\"https:\/\/www.manuelmarangoni.it\/images\/zip_logo-80x80.png\" \/><\/div>\n<div style=\"float: left; width: 350px; padding-top: 30px; font-size: 14px;\"><strong><a title=\"PHP - Censura o sostituire una serie di parole con un'altra\" href=\"https:\/\/www.manuelmarangoni.it\/download_script.php?idfile=39&amp;link=https:\/\/www.manuelmarangoni.it\/sir-bit\/1980\/php-censura-o-sostituire-una-serie-di-parole-con-unaltra\" target=\"_blank\">Scarica gli script di questo articolo in formato zip<\/a><\/strong><\/div>\n<\/div>\n<h3 style=\"margin-top: 50px; margin-bottom: 20px; color: #c00;\">La funzione di sostituzione stringa<\/h3>\n<p>La funzione permette il passaggio di due parametri e di un terzo opzionale.<\/p>\n<p><em>$string<\/em> \u00e8 il testo da analizzare, dove presumibilmente potrebbero trovarsi le parole da censurare.<\/p>\n<p><em>$bad_words<\/em> \u00e8 l&#8217; array contenente <strong>le stringhe da convertire<\/strong>.<\/p>\n<p><em>$new_string<\/em> \u00e8 la stringa con cui le parole da censurare saranno convertite. E&#8217; un parametro opzionale: se non passato, le stringhe di $bad_words saranno semplicemente <strong>oscurate con #<\/strong> (uno per carattere).<\/p>\n<p><script type=\"text\/javascript\">\/\/ <![CDATA[\ngoogle_ad_client = \"ca-pub-9984963620559913\"; \/* Sir Bit - post singolo rettangolare *\/ google_ad_slot = \"5334366715\"; google_ad_width = 336; google_ad_height = 280;\n\/\/ ]]><\/script><br \/>\n<script type=\"text\/javascript\" src=\"http:\/\/pagead2.googlesyndication.com\/pagead\/show_ads.js\">\/\/ <![CDATA[\n\n\n\/\/ ]]><\/script><\/p>\n<div class=\"wp_syntax\">\n<table>\n<tbody>\n<tr>\n<td class=\"line_numbers\">\n<pre>1\r\n2\r\n3\r\n4\r\n5\r\n6\r\n7\r\n8\r\n9\r\n10\r\n11\r\n12\r\n13\r\n14\r\n15\r\n16\r\n17\r\n18\r\n19\r\n20\r\n21\r\n22\r\n23\r\n24\r\n25\r\n26\r\n27\r\n28\r\n29<\/pre>\n<\/td>\n<td class=\"code\">\n<pre class=\"php\" style=\"font-family: monospace;\"><span style=\"color: #000000; font-weight: bold;\">function<\/span> replace_words<span style=\"color: #009900;\">(<\/span> <span style=\"color: #000088;\">$string<\/span> <span style=\"color: #339933;\">=<\/span> <span style=\"color: #0000ff;\">''<\/span><span style=\"color: #339933;\">,<\/span> <span style=\"color: #000088;\">$bad_words<\/span><span style=\"color: #339933;\">,<\/span> <span style=\"color: #000088;\">$new_string<\/span> <span style=\"color: #339933;\">=<\/span> <span style=\"color: #0000ff;\">''<\/span> <span style=\"color: #009900;\">)<\/span><span style=\"color: #009900;\">{<\/span>\r\n\r\n\t<span style=\"color: #666666; font-style: italic;\">\/\/controllo di sicurezza: $bad_words deve essere un array<\/span>\r\n\t<span style=\"color: #b1b100;\">if<\/span><span style=\"color: #009900;\">(<\/span><span style=\"color: #339933;\">!<\/span><span style=\"color: #990000;\">is_array<\/span><span style=\"color: #009900;\">(<\/span><span style=\"color: #000088;\">$bad_words<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #009900;\">{<\/span>\r\n\t\t<span style=\"color: #b1b100;\">return<\/span> <span style=\"color: #000088;\">$string<\/span><span style=\"color: #339933;\">;<\/span>\r\n\t<span style=\"color: #009900;\">}<\/span>\r\n\r\n\t<span style=\"color: #000088;\">$string<\/span> <span style=\"color: #339933;\">=<\/span> <span style=\"color: #0000ff;\">' '<\/span><span style=\"color: #339933;\">.<\/span><span style=\"color: #000088;\">$string<\/span><span style=\"color: #339933;\">.<\/span><span style=\"color: #0000ff;\">' '<\/span><span style=\"color: #339933;\">;<\/span>\r\n\r\n\t<span style=\"color: #666666; font-style: italic;\">\/\/delimitatori per caratteri non Unicode<\/span>\r\n\t<span style=\"color: #000088;\">$delimiter<\/span> <span style=\"color: #339933;\">=<\/span> <span style=\"color: #0000ff;\">'[-_\\'\\\"`(){}&lt;&gt;\\[\\]|!?@#%&amp;,.:;^~*+=\\\/ 0-9\\n\\r\\t]'<\/span><span style=\"color: #339933;\">;<\/span>\r\n\r\n\t<span style=\"color: #b1b100;\">foreach<\/span><span style=\"color: #009900;\">(<\/span><span style=\"color: #000088;\">$bad_words<\/span> <span style=\"color: #b1b100;\">as<\/span> <span style=\"color: #000088;\">$value<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #009900;\">{<\/span>\r\n\r\n\t\t<span style=\"color: #666666; font-style: italic;\">\/\/stringhe sostituite da un'altra stringa<\/span>\r\n\t\t<span style=\"color: #b1b100;\">if<\/span><span style=\"color: #009900;\">(<\/span><span style=\"color: #000088;\">$new_string<\/span> <span style=\"color: #339933;\">!=<\/span> <span style=\"color: #0000ff;\">''<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #009900;\">{<\/span>\r\n\t\t\t<span style=\"color: #000088;\">$string<\/span> <span style=\"color: #339933;\">=<\/span> <span style=\"color: #990000;\">preg_replace<\/span><span style=\"color: #009900;\">(<\/span><span style=\"color: #0000ff;\">\"\/(<span style=\"color: #006699; font-weight: bold;\">{$delimiter}<\/span>)(\"<\/span><span style=\"color: #339933;\">.<\/span><span style=\"color: #990000;\">str_replace<\/span><span style=\"color: #009900;\">(<\/span><span style=\"color: #0000ff;\">'\\*'<\/span><span style=\"color: #339933;\">,<\/span> <span style=\"color: #0000ff;\">'\\w*?'<\/span><span style=\"color: #339933;\">,<\/span> <span style=\"color: #990000;\">preg_quote<\/span><span style=\"color: #009900;\">(<\/span><span style=\"color: #000088;\">$value<\/span><span style=\"color: #339933;\">,<\/span> <span style=\"color: #0000ff;\">'\/'<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #339933;\">.<\/span><span style=\"color: #0000ff;\">\")(<span style=\"color: #006699; font-weight: bold;\">{$delimiter}<\/span>)\/i\"<\/span><span style=\"color: #339933;\">,<\/span> <span style=\"color: #0000ff;\">\"<span style=\"color: #000099; font-weight: bold;\">\\\\<\/span>1<span style=\"color: #006699; font-weight: bold;\">{$new_string}<\/span><span style=\"color: #000099; font-weight: bold;\">\\\\<\/span>3\"<\/span><span style=\"color: #339933;\">,<\/span> <span style=\"color: #000088;\">$string<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #339933;\">;<\/span>\r\n\r\n\t\t<span style=\"color: #666666; font-style: italic;\">\/\/caratteri sostituiti con #\t<\/span>\r\n\t\t<span style=\"color: #009900;\">}<\/span><span style=\"color: #b1b100;\">else<\/span><span style=\"color: #009900;\">{<\/span>\r\n\t\t\t<span style=\"color: #000088;\">$string<\/span> <span style=\"color: #339933;\">=<\/span> <span style=\"color: #990000;\">preg_replace<\/span><span style=\"color: #009900;\">(<\/span><span style=\"color: #0000ff;\">\"\/(<span style=\"color: #006699; font-weight: bold;\">{$delimiter}<\/span>)(\"<\/span><span style=\"color: #339933;\">.<\/span><span style=\"color: #990000;\">str_replace<\/span><span style=\"color: #009900;\">(<\/span><span style=\"color: #0000ff;\">'\\*'<\/span><span style=\"color: #339933;\">,<\/span> <span style=\"color: #0000ff;\">'\\w*?'<\/span><span style=\"color: #339933;\">,<\/span> <span style=\"color: #990000;\">preg_quote<\/span><span style=\"color: #009900;\">(<\/span><span style=\"color: #000088;\">$value<\/span><span style=\"color: #339933;\">,<\/span> <span style=\"color: #0000ff;\">'\/'<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #339933;\">.<\/span><span style=\"color: #0000ff;\">\")(<span style=\"color: #006699; font-weight: bold;\">{$delimiter}<\/span>)\/ie\"<\/span><span style=\"color: #339933;\">,<\/span> <span style=\"color: #0000ff;\">\"'<span style=\"color: #000099; font-weight: bold;\">\\\\<\/span>1'.str_repeat('#', strlen('<span style=\"color: #000099; font-weight: bold;\">\\\\<\/span>2')).'<span style=\"color: #000099; font-weight: bold;\">\\\\<\/span>3'\"<\/span><span style=\"color: #339933;\">,<\/span> <span style=\"color: #000088;\">$string<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #339933;\">;<\/span>\r\n\r\n\t\t<span style=\"color: #009900;\">}<\/span>\r\n\r\n\t<span style=\"color: #009900;\">}<\/span>\r\n\r\n\t<span style=\"color: #b1b100;\">return<\/span> <span style=\"color: #990000;\">trim<\/span><span style=\"color: #009900;\">(<\/span><span style=\"color: #000088;\">$string<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #339933;\">;<\/span>\r\n\r\n<span style=\"color: #009900;\">}<\/span><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<h3 style=\"margin-top: 50px; margin-bottom: 20px; color: #c00;\">Esempio d&#8217;uso<\/h3>\n<p>Di seguito prendiamo come esempio un testo generico, un array di due parole da censurare e una breve stringa con cui sostituire le parole:<\/p>\n<div class=\"wp_syntax\">\n<table>\n<tbody>\n<tr>\n<td class=\"line_numbers\">\n<pre>1\r\n2\r\n3<\/pre>\n<\/td>\n<td class=\"code\">\n<pre class=\"php\" style=\"font-family: monospace;\"><span style=\"color: #000088;\">$text<\/span> <span style=\"color: #339933;\">=<\/span> <span style=\"color: #0000ff;\">\"Qualunque bug sufficientemente avanzato \u00e8 indistinguibile da una caratteristica del software.\"<\/span><span style=\"color: #339933;\">;<\/span>\r\n<span style=\"color: #000088;\">$no_words<\/span> <span style=\"color: #339933;\">=<\/span> <span style=\"color: #990000;\">array<\/span><span style=\"color: #009900;\">(<\/span><span style=\"color: #0000ff;\">'sufficientemente'<\/span><span style=\"color: #339933;\">,<\/span> <span style=\"color: #0000ff;\">'indistinguibile'<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #339933;\">;<\/span>\r\n<span style=\"color: #000088;\">$string<\/span> <span style=\"color: #339933;\">=<\/span> <span style=\"color: #0000ff;\">' [eliminata] '<\/span><span style=\"color: #339933;\">;<\/span><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p><strong>Ogni occorrenza<\/strong> di \u00absufficientemente\u00bb e di \u00abindistinguibile\u00bb sar\u00e0 sostituira con \u00ab [eliminata] \u00bb (notare spazi laterali compresi).<\/p>\n<p>I risultati saranno i seguenti:<\/p>\n<div class=\"wp_syntax\">\n<table>\n<tbody>\n<tr>\n<td class=\"line_numbers\">\n<pre>1\r\n2\r\n3\r\n4\r\n5<\/pre>\n<\/td>\n<td class=\"code\">\n<pre class=\"php\" style=\"font-family: monospace;\"><span style=\"color: #666666; font-style: italic;\">\/\/ \" Qualunque bug [eliminata] avanzato \u00e8 [eliminata] da una caratteristica del software. \"<\/span>\r\n<span style=\"color: #b1b100;\">echo<\/span> replace_words<span style=\"color: #009900;\">(<\/span><span style=\"color: #000088;\">$text<\/span><span style=\"color: #339933;\">,<\/span> <span style=\"color: #000088;\">$no_words<\/span><span style=\"color: #339933;\">,<\/span> <span style=\"color: #000088;\">$string<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #339933;\">;<\/span>\r\n\r\n<span style=\"color: #666666; font-style: italic;\">\/\/ \" Qualunque bug ################ avanzato \u00e8 ############### da una caratteristica del software. \"<\/span>\r\n<span style=\"color: #b1b100;\">echo<\/span> replace_words<span style=\"color: #009900;\">(<\/span><span style=\"color: #000088;\">$text<\/span><span style=\"color: #339933;\">,<\/span> <span style=\"color: #000088;\">$no_words<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #339933;\">;<\/span><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Una funzione da scaricare completa di esempio per sostituire, all&#8217;interno di un testo, una serie di parole passate con un&#8217;altra parola, oppure per censurarle.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[78],"tags":[4,6],"class_list":["post-1980","post","type-post","status-publish","format-standard","hentry","category-php-mysql","tag-php","tag-script","cat-78-id"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.manuelmarangoni.it\/sir-bit\/wp-json\/wp\/v2\/posts\/1980"}],"collection":[{"href":"https:\/\/www.manuelmarangoni.it\/sir-bit\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.manuelmarangoni.it\/sir-bit\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.manuelmarangoni.it\/sir-bit\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.manuelmarangoni.it\/sir-bit\/wp-json\/wp\/v2\/comments?post=1980"}],"version-history":[{"count":5,"href":"https:\/\/www.manuelmarangoni.it\/sir-bit\/wp-json\/wp\/v2\/posts\/1980\/revisions"}],"predecessor-version":[{"id":1986,"href":"https:\/\/www.manuelmarangoni.it\/sir-bit\/wp-json\/wp\/v2\/posts\/1980\/revisions\/1986"}],"wp:attachment":[{"href":"https:\/\/www.manuelmarangoni.it\/sir-bit\/wp-json\/wp\/v2\/media?parent=1980"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.manuelmarangoni.it\/sir-bit\/wp-json\/wp\/v2\/categories?post=1980"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.manuelmarangoni.it\/sir-bit\/wp-json\/wp\/v2\/tags?post=1980"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}