{"id":4317,"date":"2021-10-17T14:08:19","date_gmt":"2021-10-17T14:08:19","guid":{"rendered":"https:\/\/www.sitekickr.com\/blog\/?p=4317"},"modified":"2021-10-17T14:09:40","modified_gmt":"2021-10-17T14:09:40","slug":"the-fastest-way-to-convert-a-text-list-to-an-array","status":"publish","type":"post","link":"https:\/\/www.sitekickr.com\/blog\/the-fastest-way-to-convert-a-text-list-to-an-array\/","title":{"rendered":"The Fastest Way to Convert a Text List to an Array"},"content":{"rendered":"\n<p>Okay, not the hottest topic in the world of dev, but a common one nonetheless!<\/p>\n\n\n\n<p>Let&#8217;s say that you&#8217;re creating a fun game that lets kids pick their favorite animal and puts a picture of their face on the animal (c&#8217;mon, million dollar idea, right).<\/p>\n\n\n\n<p>You&#8217;ll start off with a select element with a list of animals to choose from. To populate that select element, you Google &#8220;list of animal names&#8221;. The first result contains a great list of animal names from A-Z.<\/p>\n\n\n\n<p>Now, how do you convert that list into HTML &lt;option&gt; tags? There are two approaches here:<\/p>\n\n\n\n<h2>Hard Coded HTML<\/h2>\n\n\n\n<p>Believe it or not, you can use your favorite spreadsheet tool to create the HTML.<\/p>\n\n\n\n<ul><li>Copy your list from the website you found (or whatever your source is) and paste it into column B of the spreadsheet.<\/li><li>In Column A, enter &lt;option&gt; and copy\/paste it to the last row.<\/li><li>In Column C, enter &lt;option&gt; and copy\/paste it to the last row (by now, you might see where I&#8217;m going with this).<\/li><li>Export the spreadsheet as a CSV file.<\/li><li>Open the CSV file in your code editor, or text editor.<\/li><li>Find\/replace the commas with, well, nothing.<\/li><li>You&#8217;ve got your HTML!<\/li><\/ul>\n\n\n\n<h2>Use a Loop in Your Scripting Language<\/h2>\n\n\n\n<p>If you&#8217;re familiar with Regular Expressions, you can use your favorite code editor&#8217;s find\/replace dialog with it&#8217;s regular expression option.<\/p>\n\n\n\n<ul><li>Copy your list from the website you found (or whatever your source is) and paste it into your code editor.<\/li><li>Open your find\/replace dialog and make sure &#8220;use regular expressions&#8221; is checked.<\/li><li>Enter <code>\\R<\/code> in the find box.  In most tools, this signifies a line break.<\/li><li>Enter <code>','<\/code> into the replace box (adjust this based on your scripting languages syntax for an array).<\/li><li>Once you do the find\/replace, you&#8217;ll notice that the list has been converted to a single line, with each option delimited by &#8216;,&#8217;<\/li><li>From there, just add a leading <code>('<\/code> and trailing <code>')<\/code>   (depending on your languages syntax) and you&#8217;ve got your array!<\/li><\/ul>\n\n\n\n<h3>The Easier Way<\/h3>\n\n\n\n<p>As a dev, I need to do this type of thing very often. So I created a little <a href=\"https:\/\/www.sitekickr.com\/tools\/line-break-remover\/\">online Line Break Remover<\/a> tool to make this quick and painless. I keep it in a folder in my bookmark bar. <\/p>\n\n\n\n<p>Just enter a single quote in the <em>Wrap lines with<\/em> field, a comma in the <em>Replace line break with character<\/em> field, then paste your list into the first text box. Voila!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A quick and easy method to turn a multi-line list of items into HTML or an array.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"amp_status":""},"categories":[4],"tags":[],"_links":{"self":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts\/4317"}],"collection":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/comments?post=4317"}],"version-history":[{"count":3,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts\/4317\/revisions"}],"predecessor-version":[{"id":4320,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts\/4317\/revisions\/4320"}],"wp:attachment":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/media?parent=4317"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/categories?post=4317"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/tags?post=4317"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}