{"id":8825,"date":"2011-07-20T15:03:04","date_gmt":"2011-07-20T13:03:04","guid":{"rendered":"https:\/\/www.dreamsworld.it\/emanuele\/?p=8825"},"modified":"2011-07-20T14:09:21","modified_gmt":"2011-07-20T12:09:21","slug":"invertire-lordine-delle-parole-di-una-stringa-in-php","status":"publish","type":"post","link":"https:\/\/www.dreamsworld.it\/emanuele\/2011-07-20\/invertire-lordine-delle-parole-di-una-stringa-in-php\/","title":{"rendered":"Invertire l&#8217;ordine delle parole di una stringa in PHP."},"content":{"rendered":"<p>Qualche giorno fa avevo scritto, per gioco, <a title=\"La chiave.\" href=\"\/\/www.dreamsworld.it\/emanuele\/2011-07-15\/la-chiave-2\/\">una frase<\/a> che <strong>iniziava dall&#8217;ultima parola<\/strong>. Come al solito per\u00f2, prima di fare questi giochi stupidi penso <em>&#8220;come potrebbe risolvere questo problema il computer?&#8221;<\/em>. Invertire tre parole di una frase \u00e8 molto semplice, invertire testi lunghi &#8211; a mano &#8211; inizia a diventare qualcosa di oneroso in termini di tempo.<\/p>\n<p>Il problema si risolve in maniera molto semplice con <strong>un paio di funzioni<\/strong>. Per amore della pulizia non ho voluto usare alcun ciclo (<em>potevo contare le parole e salvarle in un&#8217;altra variabile partendo dall&#8217;ultima<\/em>). In PHP per\u00f2 esiste <a title=\"PHP Manual - array_reverse\" href=\"http:\/\/php.net\/manual\/en\/function.array-reverse.php\" target=\"_blank\">array_reverse<\/a>, una funzione che accetta in ingresso un array e <strong>lo ordina in maniera opposta<\/strong>.<\/p>\n<p>Cos\u00ec, in poche righe, ho trasformato la stringa in un array, l&#8217;ho ribaltata e l&#8217;ho ricomposta. \ud83d\ude42<\/p>\n<pre lang=\"php\" line=\"1\">\/\/ Codice PHP\r\n$frase = \"Questa stringa verr\u00e0 ribaltata!\";\r\n$i = explode(' ', $frase);\r\n$reverse = array_reverse($i);\r\n$risultato = implode($reverse,' ');\r\necho $risultato;\r\n\/\/ $risultato = ribaltata! verr\u00e0 stringa Questa<\/pre>\n<p>Semplice, facile ed indolore. Anzi: <em>indolore. ed facile, Semplice<\/em> \ud83d\ude42<\/p>\n<p><strong>Emanuele<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Qualche giorno fa avevo scritto, per gioco, una frase che iniziava dall&#8217;ultima parola. Come al solito per\u00f2, prima di fare questi giochi stupidi penso &#8220;come potrebbe risolvere questo problema il computer?&#8221;. Invertire tre parole di una frase \u00e8 molto semplice, &hellip; <a href=\"https:\/\/www.dreamsworld.it\/emanuele\/2011-07-20\/invertire-lordine-delle-parole-di-una-stringa-in-php\/\">Leggi ancora &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"activitypub_content_warning":"","activitypub_content_visibility":"","activitypub_max_image_attachments":4,"activitypub_interaction_policy_quote":"anyone","activitypub_status":"","footnotes":""},"categories":[3],"tags":[653,640,496,1582,242,478,611],"class_list":["post-8825","post","type-post","status-publish","format-standard","hentry","category-computer","tag-guide","tag-how-to","tag-php","tag-programmare","tag-programmazione","tag-scrivere","tag-testo"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.dreamsworld.it\/emanuele\/wp-json\/wp\/v2\/posts\/8825","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dreamsworld.it\/emanuele\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dreamsworld.it\/emanuele\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dreamsworld.it\/emanuele\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dreamsworld.it\/emanuele\/wp-json\/wp\/v2\/comments?post=8825"}],"version-history":[{"count":0,"href":"https:\/\/www.dreamsworld.it\/emanuele\/wp-json\/wp\/v2\/posts\/8825\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.dreamsworld.it\/emanuele\/wp-json\/wp\/v2\/media?parent=8825"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dreamsworld.it\/emanuele\/wp-json\/wp\/v2\/categories?post=8825"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dreamsworld.it\/emanuele\/wp-json\/wp\/v2\/tags?post=8825"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}