{"id":1425,"date":"2013-06-11T09:44:49","date_gmt":"2013-06-11T07:44:49","guid":{"rendered":"http:\/\/blog.sfsoft.it\/?p=1425"},"modified":"2013-06-11T09:44:49","modified_gmt":"2013-06-11T07:44:49","slug":"mysql-rimuovere-i-tag-html-da-un-campo","status":"publish","type":"post","link":"http:\/\/www.sfsoft.it\/blog\/2013\/06\/11\/mysql-rimuovere-i-tag-html-da-un-campo\/","title":{"rendered":"MySQL: rimuovere i TAG HTML da un campo"},"content":{"rendered":"<p>Usando nei progetti componenti tipo il <em>TinyMCE<\/em> o simili che permettono di creare contenuti formattati a piacere si ha anche il problema che in un campo testo ci possano essere anche i <em>TAG HTML<\/em> e magari ricercando ad esempio la parola <em>STRONG<\/em> o <em>TITLE<\/em> vengano restituiti risultati che poco centrano con i reali contenuti.<\/p>\n<p>Una soluzione \u00e8 quella di creare una funzione in <em>MySQL<\/em> che si occupi di rimuovere questi <em>TAG<\/em> in maniera che la ricerca possa avvenire solo sui contenuti puri.<\/p>\n<p>Sul <em>MySQL<\/em> eseguiamo questi comandi:<\/p>\n<pre lang=\"mysql\">DELIMITER $$\r\n\r\nDROP FUNCTION IF EXISTS `STRIP_TAGS`$$\r\n\r\nCREATE FUNCTION `STRIP_TAGS`( Dirty varchar(4000) ) RETURNS varchar(4000)\r\nDETERMINISTIC\r\nBEGIN\r\n  DECLARE iStart, iEnd, iLength int;\r\n  WHILE Locate( '&lt;', Dirty ) &gt; 0 And Locate( '&gt;', Dirty, Locate( '&lt;', Dirty )) &gt; 0 DO\r\n    BEGIN\r\n      SET iStart = Locate( '&lt;', Dirty ), iEnd = Locate( '&gt;', Dirty, Locate('&lt;', Dirty ));\r\n      SET iLength = ( iEnd - iStart) + 1;\r\n      IF iLength &gt; 0 THEN\r\n        BEGIN\r\n          SET Dirty = Insert( Dirty, iStart, iLength, '');\r\n        END;\r\n      END IF;\r\n    END;\r\n  END WHILE;\r\n  RETURN Dirty;\r\nEND$$\r\nDELIMITER ;<\/pre>\n<p>Per ottenere il campo pulito dai <em><span style=\"text-decoration: underline;\">TAG<\/span><\/em> \u00e8 sufficiente dentro la <em>SQL<\/em> richiamare la funzione appena creata:<\/p>\n<pre lang=\"mysql\">SELECT * FROM mytable WHERE STRIP_TAGS(myfield) LIKE '%title%';<\/pre>\n<p>Ora i risultati saranno solo riferiti ai reali contenuti del campo e non ai <em>TAG HTML<\/em>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Usando nei progetti componenti tipo il TinyMCE o simili che permettono di creare contenuti formattati a piacere si ha anche il problema che in un campo testo ci possano essere anche i TAG HTML e magari ricercando ad esempio la parola STRONG o TITLE vengano restituiti risultati che poco centrano con i reali contenuti. Una [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[15,44],"tags":[16,41],"_links":{"self":[{"href":"http:\/\/www.sfsoft.it\/blog\/wp-json\/wp\/v2\/posts\/1425"}],"collection":[{"href":"http:\/\/www.sfsoft.it\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.sfsoft.it\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.sfsoft.it\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.sfsoft.it\/blog\/wp-json\/wp\/v2\/comments?post=1425"}],"version-history":[{"count":2,"href":"http:\/\/www.sfsoft.it\/blog\/wp-json\/wp\/v2\/posts\/1425\/revisions"}],"predecessor-version":[{"id":1427,"href":"http:\/\/www.sfsoft.it\/blog\/wp-json\/wp\/v2\/posts\/1425\/revisions\/1427"}],"wp:attachment":[{"href":"http:\/\/www.sfsoft.it\/blog\/wp-json\/wp\/v2\/media?parent=1425"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.sfsoft.it\/blog\/wp-json\/wp\/v2\/categories?post=1425"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.sfsoft.it\/blog\/wp-json\/wp\/v2\/tags?post=1425"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}