Archive for January 3rd, 2007

Counting Letters In A String With PHP

Recently asked on Yahoo! Answers:

Using PHP to show how many letters in a word?

Is there a way to find out how many of a certain letter are in variables with php?
e.g. $5 = ‘dinosaurs’;

So it somehow echos how many of each letter there is?
e.g.
1-d
1-i
1-n
1-o
2-s
1-a
1-u
1-r

There is a built-in PHP function called count_chars(). However, it will count everything — spaces, numbers, punctuation, etc. It also builds in the ability to select all characters, only those characters that exist in the string more than once and only those characters that don’t appear in a string. It doesn’t really work the way the questioner asked, so we’ll proceed with our own function.

This is easily done by making a function that will store, in an associative array, all the letters present in the string, plus the count for each.

Read the rest of this entry »

Retaining Selections From A Single Select Box In Two Multiple Select Boxes Via JavaScript / DOM

Recently asked on Yahoo! Answers:

Question on Java script?

I have one html page which contain one select and two multi select box.when I select something from select menu(Ex:a,b,c) the corresponding items(a1,a2,a3….or b1,b2,b3….) arecoming in multiselect box.from the first multiselect box1 i am able to transfer the selected datas to multiselect box2.My problem is to retain the datas that are in the multiselect boxes for a particular selction in select box.These values should retain when every time I circulate through the select items.How will I do that.

I assume from this question what the user wants is this:

  • A single-choice select box (combo box) is displayed; it has several options.
  • Two additional multiple select boxes (list boxes) are displayed; they have some combination of options which match the choices in the combo box. (For the purposes of this example, I’ll add a value in one of these list boxes that doesn’t appear in the combo box. Also, the items in the two list boxes won’t be the same; one will match all available options in the combo box; the second, only a few.)
  • As the user makes choices with the combo box, the corresponding options in the list boxes are highlighted; every time a new selection is made in a combo box, that selection is also selected in the matching list box options. Previously selected items are never removed. (For the purpose of this demo, I will add a button that clears all previous selections in a fell swoop.)

The technique here is very straightforward; we simply use a global array to store all the choices made so far from the combo box, then use that array to choose what is selected in the list boxes.

Read the rest of this entry »