UNPKG

phpjs

Version:

php.js offers community built php functions in javascript

237 lines (184 loc) 7.77 kB
<!-- Generated by Rakefile:build --> <strong> <a href="http://an3m1.com/" rel="nofollow">???? ????????</a> </strong> on 2012-04-04 14:37:00 <br /> If I might —perhaps you should consider adding a few images. I don’t mean to disrespect what you’ve said ; its very enlightening, indeed. However, I think would respond to it more positively if they could be something tangible to your ideas <hr /> <strong> acebone </strong> on 2012-03-15 16:02:35 <br /> This will not uppercase foreign/extended chars, in order to do that simply replace [a-z] with [^\s] in the regex, so that the function looks like this: <pre><code> function ucwords (str) { // Uppercase the first character of every word in a string // // version: 1109.2015 // discuss at: http://phpjs.org/functions/ucwords // + 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(/^([^\s])|\s+([^\s])/g, function ($1) { return $1.toUpperCase(); }); </code></pre> <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2010-09-08 17:56:25 <br /> @ James: Thanks for your contribution! I modified the regex some more so it will only try to uppercase small letters (why try to uppercase '{' or '.' as well), and to support words separated by more than 1 spaces. <hr /> <strong> <a href="http://www.james-bell.co.uk" rel="nofollow">James</a> </strong> on 2010-08-11 13:19:22 <br /> I have modified the regex used so that it ignores spaces and numbers, new to regex so any hints, tips, changes etc.. are welcome <pre><code> function ucwords(str) { str = str.toLowerCase(); //Optional return (str).replace(/^([^\s0-9])|\s([^\s0-9])/g, function (l) { return l.toUpperCase(); }); } </code></pre> <hr /> <strong> <a href="http://brett-zamir.me" rel="nofollow">Brett Zamir</a> </strong> on 2010-04-28 04:27:05 <br /> @Jerry: A nice idea, but that only works with English letters. If we could boil it down a bit, we could incorporate XRegExp.com along with its excellent Unicode plug-in to determine whether something was lower case or not, but that would probably end up costing more performance to check for all the Unicode values (and it would need to take into account some languages which deviate from the Unicode case mapings). Off topic slightly, our function should really be made to work with locale_set_default (whenever that gets implemented!), at least when the &quot;unicode.semantics&quot; ini is on. <hr /> <strong> Jerry </strong> on 2010-04-27 23:30:06 <br /> I prefer this slightly modified version as replacement function should ONLY be called when the regex finds suitable letters instead of suitable characters. <pre><code> function ucwords(str) { return (str + '').replace(/^[a-z]|\s[a-z]/g, function ($1) { return $1.toUpperCase(); } ); } </code></pre> <hr /> <strong> <a href="http://brett-zamir.me" rel="nofollow">Brett Zamir</a> </strong> on 2010-01-29 02:36:21 <br /> @Robin: It wasn't hostility at all! I just didn't see your notice, so I was just stating what I said in a matter of fact way, not with any sarcasm or anything. And as you say, it was my mistake for missing your additional comment. We're really sorry if you were met with hostility earlier, but sometimes I think the lack of opportunity for clarification of intent may mistakenly give that impression; if you were met with genuine hostility, or we could have been more clear or more polite, our apologies certainly for that! And yes, we are eager to make JSLint fixes, so thank you very much for the function fixes! I'll see about applying them in Git now... <hr /> <strong> Robin </strong> on 2010-01-28 18:24:04 <br /> @Brett: Thanks for the feedback, but if you read the rest of my comment, you'll see I did not intend to change the function in any way, except to make it conform to the JSLint conventions, as shown by the &quot;Open syntax issues.&quot; &quot;Eventually we want all code to pass or at least take into consideration most fixes suggested by JsLint, following this JsLint configuration we’ve decided on.&quot; If the previous statement no longer stands, I suggest you remove it from your website. This isn't the first time I've tried to contribute to this site, and been met by hostility but thankfully, this will be the last, you may commence the group sigh of relief. <hr /> <strong> <a href="http://brett-zamir.me" rel="nofollow">Brett Zamir</a> </strong> on 2010-01-26 02:47:17 <br /> @Robin: Thanks for the submission, but besides differing in a variable name, your function looks identical to what we already have... <hr /> <strong> Robin </strong> on 2010-01-25 18:41:59 <br /> <pre><code> function ucwords(str) { return (str + '').replace(/^(.)|\s(.)/g, function (m) { return m.toUpperCase(); }); } </code></pre> Now conforms to JSLint syntax conventions. <hr /> <strong> <a href="http://www.powerinspired.com" rel="nofollow">Tony</a> </strong> on 2010-01-11 09:52:26 <br /> Great function - and picked up #1 on google search for what I was after. Saved me a host of time. Thanks <hr /> <strong> Alessandro </strong> on 2008-07-31 18:20:39 <br /> It doesn't work to me on Firefox 3, it returns the same input sentence (I've used a complete uppercase sentence)... <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2008-01-31 19:43:34 <br /> @ _argos: That's good enough for me :) I'm replacing the function and leave the original one here for future reference: <pre><code> function ucwords( str ) { // http://kevin.vanzonneveld.net // + original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com) // * example 1: ucwords('kevin van zonneveld'); // * returns 1: 'Kevin Van Zonneveld' return str.toLowerCase().replace(/\w+/g, function(s){ return s.charAt(0).toUpperCase() + s.substr(1); }); } </code></pre> <hr /> <strong> _argos </strong> on 2008-01-31 18:32:33 <br /> Hi Kevin, I'm making a benchmark of this function using RegExp an is 25%-30% more faster than your original function, this is the bechmark that I try: <pre><code> // _argos's function function ucwords_w ( str ) { return str.replace(/^(.)|\s(.)/g, function ( $1 ) { return $1.toUpperCase ( ); } ); } // Original function function ucwords( str ) { return str.toLowerCase().replace(/\w+/g, function(s){ return s.charAt(0).toUpperCase() + s.substr(1); }); } // BENCHMARK var start; start=new Date(); iterations = 10000; while ( iterations-- ) { ucwords('Kevin van Zonneveld'); ucwords('Waldo Malqui Silva'); ucwords('Alexa Valentina Malqui Cordova'); } console.log('strrev = ' + ( (new Date())-start)) start=new Date(); iterations = 10000; while ( iterations-- ) { ucwords_w('Kevin van Zonneveld'); ucwords_w('Waldo Malqui Silva'); ucwords_w('Alexa Valentina Malqui Cordova'); } console.log( 'strrev_w = ' + ( (new Date())-start )) </code></pre> <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2008-01-19 21:02:08 <br /> @ Lars: You're welcome ;) <hr /> <strong> Lars </strong> on 2008-01-19 09:31:45 <br /> thanks! <hr />