---
layout: page
title: "JavaScript count_chars function"
comments: true
sharing: true
footer: true
alias:
- /functions/view/count_chars:376
- /functions/view/count_chars
- /functions/view/376
- /functions/count_chars:376
- /functions/376
---
<!-- Generated by Rakefile:build -->
A JavaScript equivalent of PHP's count_chars

{% codeblock strings/count_chars.js lang:js https://raw.github.com/kvz/phpjs/master/functions/strings/count_chars.js raw on github %}
function count_chars (str, mode) {
  // From: http://phpjs.org/functions
  // +   original by: Ates Goral (http://magnetiq.com)
  // +    tweaked by: Jack
  // +   bugfixed by: Onno Marsman
  // +      input by: Brett Zamir (http://brett-zamir.me)
  // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  // +    revised by: Theriault
  // *     example 1: count_chars("Hello World!", 3);
  // *     returns 1: "!HWdelor"
  // *     example 2: count_chars("Hello World!", 1);
  // *     returns 2: {32:1,33:1,72:1,87:1,100:1,101:1,108:3,111:2,114:1}
  var result = {},
    resultArr = [],
    i;

  str = ('' + str).split('').sort().join('').match(/(.)\1*/g);

  if ((mode & 1) == 0) {
    for (i = 0; i != 256; i++) {
      result[i] = 0;
    }
  }

  if (mode === 2 || mode === 4) {

    for (i = 0; i != str.length; i += 1) {
      delete result[str[i].charCodeAt(0)];
    }
    for (i in result) {
      result[i] = (mode === 4) ? String.fromCharCode(i) : 0;
    }

  } else if (mode === 3) {

    for (i = 0; i != str.length; i += 1) {
      result[i] = str[i].slice(0, 1);
    }

  } else {

    for (i = 0; i != str.length; i += 1) {
      result[str[i].charCodeAt(0)] = str[i].length;
    }

  }
  if (mode < 3) {
    return result;
  }

  for (i in result) {
    resultArr.push(result[i]);
  }
  return resultArr.join('');
}
{% endcodeblock %}

 - [Raw function on GitHub](https://github.com/kvz/phpjs/blob/master/functions/strings/count_chars.js)

Please note that php.js uses JavaScript objects as substitutes for PHP arrays, they are 
the closest match to this hashtable-like data structure. 

Please also note that php.js offers community built functions and goes by the 
[McDonald's Theory](https://medium.com/what-i-learned-building/9216e1c9da7d). We'll put online 
functions that are far from perfect, in the hopes to spark better contributions. 
Do you have one? Then please just: 

 - [Edit on GitHub](https://github.com/kvz/phpjs/edit/master/functions/strings/count_chars.js)

### Example 1
This code
{% codeblock lang:js example %}
count_chars("Hello World!", 3);
{% endcodeblock %}

Should return
{% codeblock lang:js returns %}
"!HWdelor"
{% endcodeblock %}

### Example 2
This code
{% codeblock lang:js example %}
count_chars("Hello World!", 1);
{% endcodeblock %}

Should return
{% codeblock lang:js returns %}
{32:1,33:1,72:1,87:1,100:1,101:1,108:3,111:2,114:1}
{% endcodeblock %}


### Other PHP functions in the strings extension
{% render_partial _includes/custom/strings.html %}
