UNPKG

phpjs

Version:

php.js offers community built php functions in javascript

73 lines (61 loc) 2.25 kB
--- layout: page title: "JavaScript ucwords function" comments: true sharing: true footer: true alias: - /functions/view/ucwords:569 - /functions/view/ucwords - /functions/view/569 - /functions/ucwords:569 - /functions/569 --- <!-- Generated by Rakefile:build --> A JavaScript equivalent of PHP's ucwords {% codeblock strings/ucwords.js lang:js https://raw.github.com/kvz/phpjs/master/functions/strings/ucwords.js raw on github %} function ucwords (str) { // From: http://phpjs.org/functions // + original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com) // + improved by: Waldo Malqui Silva // + bugfixed by: Onno Marsman // + improved by: Robin // + input by: James (http://www.james-bell.co.uk/) // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) // * example 1: ucwords('kevin van zonneveld'); // * returns 1: 'Kevin Van Zonneveld' // * example 2: ucwords('HELLO WORLD'); // * returns 2: 'HELLO WORLD' return (str + '').replace(/^([a-z\u00E0-\u00FC])|\s+([a-z\u00E0-\u00FC])/g, function ($1) { return $1.toUpperCase(); }); } {% endcodeblock %} - [Raw function on GitHub](https://github.com/kvz/phpjs/blob/master/functions/strings/ucwords.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/ucwords.js) ### Example 1 This code {% codeblock lang:js example %} ucwords('kevin van zonneveld'); {% endcodeblock %} Should return {% codeblock lang:js returns %} 'Kevin Van Zonneveld' {% endcodeblock %} ### Example 2 This code {% codeblock lang:js example %} ucwords('HELLO WORLD'); {% endcodeblock %} Should return {% codeblock lang:js returns %} 'HELLO WORLD' {% endcodeblock %} ### Other PHP functions in the strings extension {% render_partial _includes/custom/strings.html %}