phpjs
Version:
58 lines (48 loc) • 1.95 kB
Markdown
layout: page
title: "JavaScript strcoll function"
comments: true
sharing: true
footer: true
alias:
- /functions/view/strcoll:789
- /functions/view/strcoll
- /functions/view/789
- /functions/strcoll:789
- /functions/789
<!-- Generated by Rakefile:build -->
A JavaScript equivalent of PHP's strcoll
{% codeblock strings/strcoll.js lang:js https://raw.github.com/kvz/phpjs/master/functions/strings/strcoll.js raw on github %}
function strcoll (str1, str2) {
// From: http://phpjs.org/functions
// + original by: Brett Zamir (http://brett-zamir.me)
// + improved by: Brett Zamir (http://brett-zamir.me)
// - depends on: setlocale
// * example 1: strcoll('a', 'b');
// * returns 1: -1
this.setlocale('LC_ALL', 0); // ensure setup of localization variables takes place
var cmp = this.php_js.locales[this.php_js.localeCategories.LC_COLLATE].LC_COLLATE;
// return str1.localeCompare(str2); // We don't use this as it doesn't allow us to control it via setlocale()
return cmp(str1, str2);
}
{% endcodeblock %}
- [Raw function on GitHub](https://github.com/kvz/phpjs/blob/master/functions/strings/strcoll.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/strcoll.js)
### Example 1
This code
{% codeblock lang:js example %}
strcoll('a', 'b');
{% endcodeblock %}
Should return
{% codeblock lang:js returns %}
-1
{% endcodeblock %}
### Other PHP functions in the strings extension
{% render_partial _includes/custom/strings.html %}