{"id":1631,"date":"2013-01-04T06:30:32","date_gmt":"2013-01-04T05:30:32","guid":{"rendered":"http:\/\/www.manuelmarangoni.it\/sir-bit\/?p=1631"},"modified":"2013-05-26T14:04:57","modified_gmt":"2013-05-26T13:04:57","slug":"tutorial-dividere-una-stringa-su-righe-di-50-caratteri","status":"publish","type":"post","link":"https:\/\/www.manuelmarangoni.it\/sir-bit\/1631\/tutorial-dividere-una-stringa-su-righe-di-50-caratteri\/","title":{"rendered":"Tutorial Php &#8211; Dividere una stringa su righe di 50 caratteri"},"content":{"rendered":"<div id=\"attachment_1641\" style=\"width: 410px\" class=\"wp-caption aligncenter\"><a class=\"lightbox\" title=\"Con una semplice funzione possiamo dividere il testo su pi\u00f9 righe, senza troncare le parole\" href=\"https:\/\/www.manuelmarangoni.it\/sir-bit\/wp-content\/uploads\/2012\/12\/Stringhe-di-elastici.jpg\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1641\" class=\"size-medium wp-image-1641 \" style=\"margin-bottom: 5px;\" title=\"Stringhe di elastici\" alt=\"Stringhe di elastici\" src=\"https:\/\/www.manuelmarangoni.it\/sir-bit\/wp-content\/uploads\/2012\/12\/Stringhe-di-elastici-400x300.jpg\" width=\"400\" height=\"300\" srcset=\"https:\/\/www.manuelmarangoni.it\/sir-bit\/wp-content\/uploads\/2012\/12\/Stringhe-di-elastici-400x300.jpg 400w, https:\/\/www.manuelmarangoni.it\/sir-bit\/wp-content\/uploads\/2012\/12\/Stringhe-di-elastici.jpg 800w\" sizes=\"(max-width: 400px) 100vw, 400px\" \/><\/a><p id=\"caption-attachment-1641\" class=\"wp-caption-text\">Con una semplice funzione possiamo dividere il testo su pi\u00f9 righe, senza troncare le parole (1)<\/p><\/div>\n<p>Poniamo il caso che abbiate un contenitore (un div, per esempio) la cui lunghezza \u00e8 pari da richiedere che ogni riga di testo non abbia pi\u00f9 di 50 caratteri. All&#8217;interno di questo contenitore, per\u00f2, dovete inserire un testo dinamico (per esempio preso da database) di cui non potete sapere la lunghezza a priori: potrebbe essere una stringa da 10 caratteri o da mille.<br \/>\nQuello che vi occorre, in casi come questo &#8211; che non sono per niente rari &#8211; \u00e8 un <strong>sistema per suddividere il testo dinamico in &#8220;parti&#8221; di 50 caratteri<\/strong>, in modo da poterlo disporre su righe diverse senza strabordare dal contenitore.<\/p>\n<p>Lo script che andiamo a vedere oggi fa esattamente questo. Si tratta di una semplice funzione che si compone di tre parametri:<br \/>\n&#8211; il testo da stampare su pi\u00f9 righe<br \/>\n&#8211; il numero di caratteri massimo che deve stare su una riga<br \/>\n&#8211; un eventuale elemento separatore con cui suddivideremo, ulteriormente, il testo passato (parametro opzionale)<\/p>\n<p>La funzione \u00e8 <strong>costruita affinch\u00e9 le parole non vengano troncate<\/strong>: se siamo arrivati a quasi 50 caratteri e la successiva parola ne contiene per esempio 10, la parola sar\u00e0 messa a capo su una nuova riga.<br \/>\nIl carattere separatore dipende dalle vostre esigenze: pu\u00f2 essere l&#8217;elemento di accapo (<br \/>\no \\n) oppure un qualsiasi altro elemento utile per tenere traccia di dove deve iniziare e finire una riga.<\/p>\n<p>Diamo un&#8217;occhiata alla funzione che, come sempre, \u00e8 commentata.<br \/>\nLo <strong>script \u00e8 liberamente scaricabile<\/strong> e personalizzabile. I commenti dovrebbero essere pi\u00f9 che sufficienti per comprendere il funzionamento del codice.<\/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=\"Script - Dividere una stringa su righe di 50 caratteri\" alt=\"Script - Dividere una stringa su righe di 50 caratteri\" 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=\"Script - Dividere una stringa su righe di 50 caratteri\" href=\"https:\/\/www.manuelmarangoni.it\/download_script.php?idfile=33&amp;link=https:\/\/www.manuelmarangoni.it\/sir-bit\/?p=1631\" target=\"_blank\">Scarica gli script di questo articolo in formato zip<\/a><\/strong><\/div>\n<\/div>\n<p><!--more--><br \/>\n<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\/\/ ]]><\/script> <\/p>\n<div class=\"wp_syntax\" style=\"clear: both;\">\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\r\n30\r\n31\r\n32\r\n33\r\n34\r\n35\r\n36\r\n37\r\n38\r\n39\r\n40\r\n41\r\n42\r\n43\r\n44\r\n45\r\n46\r\n47\r\n48\r\n49\r\n50<\/pre>\n<\/td>\n<td class=\"code\">\n<pre class=\"php\" style=\"font-family: monospace;\"><span style=\"color: #666666; font-style: italic;\">\/\/- $stringa: stringa da dividere<\/span>\r\n<span style=\"color: #666666; font-style: italic;\">\/\/- $lenght: lunghezza di ogni stringa<\/span>\r\n<span style=\"color: #666666; font-style: italic;\">\/\/- $separatore: carattere si separazione base per le stringhe (per default \\n); per l'html si pu\u00f2 usare &lt;br \/&gt;<\/span>\r\n<span style=\"color: #000000; font-weight: bold;\">function<\/span> dividi_testo<span style=\"color: #009900;\">(<\/span><span style=\"color: #000088;\">$stringa<\/span><span style=\"color: #339933;\">,<\/span> <span style=\"color: #000088;\">$lenght<\/span><span style=\"color: #339933;\">,<\/span> <span style=\"color: #000088;\">$separatore<\/span><span style=\"color: #339933;\">=<\/span><span style=\"color: #0000ff;\">\"<span style=\"color: #000099; font-weight: bold;\">\\n<\/span>\"<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #009900;\">{<\/span>\r\n\r\n\t<span style=\"color: #666666; font-style: italic;\">\/\/se la stringa non \u00e8 vuota ed \u00e8 pi\u00f9 lunga di $lenght, la divido<\/span>\r\n\t<span style=\"color: #b1b100;\">if<\/span><span style=\"color: #009900;\">(<\/span><span style=\"color: #000088;\">$stringa<\/span><span style=\"color: #339933;\">!=<\/span><span style=\"color: #0000ff;\">\"\"<\/span> and <span style=\"color: #990000;\">strlen<\/span><span style=\"color: #009900;\">(<\/span><span style=\"color: #000088;\">$stringa<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #339933;\">&gt;<\/span><span style=\"color: #000088;\">$lenght<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #009900;\">{<\/span>\r\n\r\n\t\t<span style=\"color: #000088;\">$stringa2<\/span><span style=\"color: #339933;\">=<\/span><span style=\"color: #0000ff;\">\"\"<\/span><span style=\"color: #339933;\">;<\/span>\r\n\t\t<span style=\"color: #666666; font-style: italic;\">\/\/la divido in base agli accapo (separatori) gi\u00e0 presenti, poi prendo ogni frammento e, se pi\u00f9 lungo di $lenght, li divido in pezzi contenenti $lenght caratteri<\/span>\r\n\t\t<span style=\"color: #000088;\">$array<\/span><span style=\"color: #339933;\">=<\/span><span style=\"color: #990000;\">explode<\/span><span style=\"color: #009900;\">(<\/span><span style=\"color: #000088;\">$separatore<\/span><span style=\"color: #339933;\">,<\/span> <span style=\"color: #000088;\">$stringa<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #339933;\">;<\/span>\r\n\r\n\t\t<span style=\"color: #666666; font-style: italic;\">\/\/prendo ogni pezzo e lo divido in base al totale di caratteri per riga<\/span>\r\n\t\t<span style=\"color: #b1b100;\">foreach<\/span><span style=\"color: #009900;\">(<\/span><span style=\"color: #000088;\">$array<\/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\t<span style=\"color: #b1b100;\">if<\/span><span style=\"color: #009900;\">(<\/span><span style=\"color: #990000;\">strlen<\/span><span style=\"color: #009900;\">(<\/span><span style=\"color: #000088;\">$value<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #339933;\">&gt;<\/span><span style=\"color: #000088;\">$lenght<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #009900;\">{<\/span>\r\n\r\n\t\t\t\t<span style=\"color: #000088;\">$inizio<\/span><span style=\"color: #339933;\">=<\/span><span style=\"color: #cc66cc;\">0<\/span><span style=\"color: #339933;\">;<\/span>\r\n\t\t\t\t<span style=\"color: #b1b100;\">while<\/span><span style=\"color: #009900;\">(<\/span><span style=\"color: #000088;\">$inizio<\/span><span style=\"color: #339933;\">&lt;<\/span><span style=\"color: #990000;\">strlen<\/span><span style=\"color: #009900;\">(<\/span><span style=\"color: #000088;\">$value<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #009900;\">{<\/span>\r\n\t\t\t\t\t<span style=\"color: #666666; font-style: italic;\">\/\/estraggo i primi tot caratteri dall'inizio<\/span>\r\n\t\t\t\t\t<span style=\"color: #000088;\">$parte1<\/span><span style=\"color: #339933;\">=<\/span><span style=\"color: #990000;\">substr<\/span><span style=\"color: #009900;\">(<\/span><span style=\"color: #000088;\">$value<\/span><span style=\"color: #339933;\">,<\/span> <span style=\"color: #000088;\">$inizio<\/span><span style=\"color: #339933;\">,<\/span> <span style=\"color: #000088;\">$lenght<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #339933;\">;<\/span>\r\n\r\n\t\t\t\t\t<span style=\"color: #666666; font-style: italic;\">\/\/controllo nel caso in cui si \u00e8 all'ultima parte di stringa (altrimenti viene troncata)<\/span>\r\n\t\t\t\t\t<span style=\"color: #b1b100;\">if<\/span><span style=\"color: #009900;\">(<\/span><span style=\"color: #000088;\">$inizio<\/span><span style=\"color: #339933;\">&gt;<\/span><span style=\"color: #990000;\">strlen<\/span><span style=\"color: #009900;\">(<\/span><span style=\"color: #000088;\">$value<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #339933;\">-<\/span><span style=\"color: #000088;\">$lenght<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #009900;\">{<\/span>\r\n\t\t\t\t\t\t<span style=\"color: #000088;\">$stringa2<\/span><span style=\"color: #339933;\">.=<\/span><span style=\"color: #000088;\">$parte1<\/span><span style=\"color: #339933;\">.<\/span><span style=\"color: #000088;\">$separatore<\/span><span style=\"color: #339933;\">;<\/span>\r\n\t\t\t\t\t\t<span style=\"color: #b1b100;\">break<\/span><span style=\"color: #339933;\">;<\/span>\t\r\n\t\t\t\t\t<span style=\"color: #009900;\">}<\/span>\r\n\r\n\t\t\t\t\t<span style=\"color: #666666; font-style: italic;\">\/\/torno al carattere prima dello spazio<\/span>\r\n\t\t\t\t\t<span style=\"color: #000088;\">$parte1<\/span><span style=\"color: #339933;\">=<\/span><span style=\"color: #990000;\">substr<\/span><span style=\"color: #009900;\">(<\/span><span style=\"color: #000088;\">$parte1<\/span><span style=\"color: #339933;\">,<\/span> <span style=\"color: #cc66cc;\">0<\/span><span style=\"color: #339933;\">,<\/span> <span style=\"color: #990000;\">strrpos<\/span><span style=\"color: #009900;\">(<\/span><span style=\"color: #000088;\">$parte1<\/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>\r\n\t\t\t\t\t<span style=\"color: #666666; font-style: italic;\">\/\/aggiorno il puntatore per ripartire dalla stringa (il +1 serve per eliminare lo spazio quando va a capo)<\/span>\r\n\t\t\t\t\t<span style=\"color: #000088;\">$inizio<\/span><span style=\"color: #339933;\">+=<\/span><span style=\"color: #990000;\">strlen<\/span><span style=\"color: #009900;\">(<\/span><span style=\"color: #000088;\">$parte1<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #339933;\">+<\/span><span style=\"color: #cc66cc;\">1<\/span><span style=\"color: #339933;\">;<\/span>\r\n\t\t\t\t\t<span style=\"color: #666666; font-style: italic;\">\/\/salvo la parte tagliata<\/span>\r\n\t\t\t\t\t<span style=\"color: #000088;\">$stringa2<\/span><span style=\"color: #339933;\">.=<\/span><span style=\"color: #000088;\">$parte1<\/span><span style=\"color: #339933;\">.<\/span><span style=\"color: #000088;\">$separatore<\/span><span style=\"color: #339933;\">;<\/span>\r\n\r\n\t\t\t\t<span style=\"color: #009900;\">}<\/span>\r\n\r\n\t\t\t<span style=\"color: #009900;\">}<\/span><span style=\"color: #b1b100;\">else<\/span>\r\n\t\t\t\t<span style=\"color: #000088;\">$stringa2<\/span><span style=\"color: #339933;\">.=<\/span><span style=\"color: #000088;\">$value<\/span><span style=\"color: #339933;\">.<\/span><span style=\"color: #000088;\">$separatore<\/span><span style=\"color: #339933;\">;<\/span>\r\n\r\n\t\t<span style=\"color: #009900;\">}<\/span>\r\n\r\n\t\t<span style=\"color: #b1b100;\">return<\/span> <span style=\"color: #990000;\">trim<\/span><span style=\"color: #009900;\">(<\/span><span style=\"color: #000088;\">$stringa2<\/span><span style=\"color: #339933;\">,<\/span> <span style=\"color: #000088;\">$separatore<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #339933;\">;<\/span>\r\n\r\n\t<span style=\"color: #009900;\">}<\/span><span style=\"color: #b1b100;\">else<\/span>\r\n\t\t<span style=\"color: #b1b100;\">return<\/span> <span style=\"color: #000088;\">$stringa<\/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<p>Per testare la funzione troverete una stringa di prova piuttosto lunga. La funzione \u00e8 quindi richiamabile in questo sistema (dove $variabile \u00e8 il testo da suddividere):<\/p>\n<div class=\"wp_syntax\">\n<table>\n<tbody>\n<tr>\n<td class=\"line_numbers\">\n<pre>1<\/pre>\n<\/td>\n<td class=\"code\">\n<pre class=\"php\" style=\"font-family: monospace;\"><span style=\"color: #b1b100;\">echo<\/span> dividi_testo<span style=\"color: #009900;\">(<\/span><span style=\"color: #000088;\">$variabile<\/span><span style=\"color: #339933;\">,<\/span> <span style=\"color: #cc66cc;\">50<\/span><span style=\"color: #339933;\">,<\/span> <span style=\"color: #0000ff;\">\"&lt;br&gt;\"<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #339933;\">;<\/span><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<div style=\"margin-top: 100px; margin-bottom: 20px;\"><strong><em>Copyright immagini<\/em><\/strong><\/div>\n<p>(1) http:\/\/artclubblog.com\/2012\/11\/19\/string-art\/<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Una funzione in php per dividere il testo su pi\u00f9 righe, senza troncare le parole. E&#8217; possibile usare un elemento separatore e un numero di caratteri per riga a piacere.<\/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-1631","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\/1631"}],"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=1631"}],"version-history":[{"count":18,"href":"https:\/\/www.manuelmarangoni.it\/sir-bit\/wp-json\/wp\/v2\/posts\/1631\/revisions"}],"predecessor-version":[{"id":1882,"href":"https:\/\/www.manuelmarangoni.it\/sir-bit\/wp-json\/wp\/v2\/posts\/1631\/revisions\/1882"}],"wp:attachment":[{"href":"https:\/\/www.manuelmarangoni.it\/sir-bit\/wp-json\/wp\/v2\/media?parent=1631"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.manuelmarangoni.it\/sir-bit\/wp-json\/wp\/v2\/categories?post=1631"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.manuelmarangoni.it\/sir-bit\/wp-json\/wp\/v2\/tags?post=1631"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}