-2

Is it possible to break a word into letters and store every letter in an array in HTML/CSS?

For example; HAT to array[1]=H, array[2]=A, array[3]=T

If there's an existing thread to this please do let me know.

Jee Garin
  • 3
  • 4
  • Yes, this is possible. `var parts = ;` or with just JS: `var parts = word.split('');` OR `var parts = ("HAT").split('');` I supose you could just do `var parts = ['H','A','T'];` if you really just want hat as an array ... ;-p – ArtisticPhoenix Oct 13 '18 at 08:56
  • Hi Magnus, thank you for your response. Normally i save my HTML files as PHP for database related activities. Someone might say that the above will be possible in HTML/CSS but not in PHP so I can take note. – Jee Garin Oct 13 '18 at 08:57
  • It's possible with PHP, but you have to JSON encode it so that it's a string representation of an array (before you can put it in javascript). That said your question is to Broad for SO. Please be more specific. – ArtisticPhoenix Oct 13 '18 at 08:59
  • Thank you very much ArtisticPhoenix for your swift response! I'll try this one out and I'll inform you immediately. Kindly post this one as an answer so I can tag it once I'm able to confirm :) – Jee Garin Oct 13 '18 at 09:01
  • Possible duplicate of [How do you get a string to a character array in JavaScript?](https://stackoverflow.com/questions/4547609/how-do-you-get-a-string-to-a-character-array-in-javascript) – Mohammad Oct 13 '18 at 09:03
  • There are no arrays in HTML and CSS. I think the HTML and CSS tags should be removed from the topic. – Alexander Oct 13 '18 at 09:15

5 Answers5

1

hmm yes, u can use JS ofcorse.

var words = "any wordds bla bla";
var array = words.split('');

that simple.

Talg123
  • 1,468
  • 1
  • 10
  • 15
1

While strings are iterables, you could use either Array.from or spread syntax ... for getting an array of chracters. Both techniques rely on the same implemented Symbol.iterator.

var string = 'HAT',
    array1 = Array.from(string),
    array2 = [...string];
    
console.log(array1);
console.log(array2);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

var sampleString = 'This is my sample string';

console.log( sampleString.split('') );
gr4viton
  • 1,434
  • 1
  • 8
  • 21
0

In javascript you can do

var parts = ("HAT").split('');

Adding PHP to that there are 2 ways

var parts = ('<?php echo "HAT"; ?>').split('');

The quoting can be a pain this way (note the quotes outside the PHP tags), and the processing takes place on the client (which you have no control of the performance specs)

Or

var parts = <?php echo json_encode(str_split('HAT')); ?>;

JSON (Javascript Object Notation) and json_encode is a way of translating complex data into a string fromat that Javascript understands. This happens server side and is generally better.

The first 2 are basically the same as far as the client goes. The last one will look like this in the source code:

var parts = ["H","A","T"];

And so it's ready to go as far as the client is concerned. This may seem like a small difference, but if you have a large amount of text, it's better to let the server handle it if possible.

It's basically a matter of where you want the processing cost to happen. Clients hardware can vary and you have no way to know what they have. The server is yours to control. So letting the client take the hit, will typically impact page loading times more then the server doing it. Unless you have a junk server and the client has a good PC (which you have no way to know that).

In any case it's a good practice to present the client with the data formatted in the way it needs to be so it can use it with as little work as possible.

ArtisticPhoenix
  • 21,464
  • 2
  • 24
  • 38
0

Use the split function with this parameter ''. (press SINGLE quote two times) The split function splits a string with another string. This other string is the parameter in the function. For example, in this string "foo,bar", if we will use the split function on this, and you want to split by a comma, we would use myString.split(','). It will return ['foo','bar']. If we separate it by an empty string, which can be in between letters, we would use .split(''). It will return ['f','o','o','b','a','r']. So you can use this to break words into letters. I put example code below.

var myString = "foobar";
var myArray = myString.split('');

// returns ['f','o','o','b','a','r']

Edit #1

This works for any character in your string, so if you want to have just the words, you can replace unwanted characters with '' first, and then use the split.

var myString = 'foo:)bar';
var myArray = myString.replace(/\:\)/, '');

//returns ['f','o','o','b','a','r'] 
Kino Bacaltos
  • 373
  • 1
  • 2
  • 16