UNPKG

phpjs

Version:

php.js offers community built php functions in javascript

233 lines (162 loc) 6.28 kB
<!-- Generated by Rakefile:build --> <strong> <a href="http://brett-zamir.me" rel="nofollow">Brett Zamir</a> </strong> on 2012-09-19 03:23:45 <br /> @Brent: Sorry for the very late reply, but is the functionality you seek already a part of PHP? You are free to share extension ideas here, but we really try to stick with PHP behavior to maintain limits on our project scope. @kernel: Thank you for the reference, but is there some functionality present in the other implementation which is not in ours? <hr /> <strong> <a href="http://brett-zamir.me" rel="nofollow">Brett Zamir</a> </strong> on 2012-09-18 16:28:11 <br /> @Dj: Fixed in Git, thanks! <hr /> <strong> Dj </strong> on 2012-08-08 23:28:13 <br /> Note that the regexp does not include the char &quot;F&quot; (uppercase), so expressions like &quot;%01.2F&quot; will not works. The function is already done to process it, but it just was missed in the regexp so it is not captured. <hr /> <strong> Brent </strong> on 2011-09-29 02:49:33 <br /> As far as adding a thousands separator, I don't think it is currently in the sprintf function code above. However, this regex does it: <pre><code> s/\d{1,3}(?=(\d{3})+(?!\d))/$&amp;,/g </code></pre> This javascript code (based on the regex) does it: <pre><code> function thousands (a){ return a.replace (/\d{1,3}(?=(\d{3})+(?!\d))/g,&quot;$&amp;,&quot;); } </code></pre> The only flaw is that it will happily keep adding commas every three characters to the right of the decimal point as well as to the left. So you have to apply it to only the integer side of the number. It works fine as-is for integers and, for example, currency (which only has max 2 decimal places). If you want to use another separator character than &quot;,&quot; make the change in the second argument to a.replace. I can't quite see how to easily add this to the sprintf function (and also the usual flag to add the thousands separator is ', which is used for something else in the code above) but there is a start for anyone who like to do it. Regex source is from: http://remysharp.com/2007/10/19/thousand-separator-regex/ <hr /> <strong> <a href="http://blog.kukawski.pl" rel="nofollow">Rafa? Kukawski</a> </strong> on 2011-01-14 07:46:33 <br /> @Dj: Thanks for your feedback. I will add the fix today. BTW. The same can be done without the need to call parseInt and isNaN. <pre><code>number = (+value) | 0;</code></pre> +value casts the data to Number, | 0 drops floating point part and if the +value gives NaN, changes it to 0. <hr /> <strong> Dj </strong> on 2011-01-14 00:07:29 <br /> A suggest: check the result of parseInt() function, and set it to 0 if is NaN <pre><code> case 'd': number = parseInt(+value, 10); if (isNaN(number) { number = 0; } </code></pre> so when a %d is replaced with a non integer value, the result is 0 instead of NaN like PHP do. Example without check NaN: result = sprintf('Number is: %d', 'non numeric'); //result is: 'Number is NaN' Example checking NaN: result = sprintf('Number is: %d', 'non numeric'); //result is: 'Number is 0' try it in php and compare <hr /> <strong> <a href="http://www.btcomic.net/" rel="nofollow">kernel</a> </strong> on 2010-10-09 18:35:14 <br /> please consult this project, that will be helpful http://www.diveintojavascript.com/projects/javascript-sprintf <hr /> <strong> <a href="http://brett-zamir.me" rel="nofollow">Brett Zamir</a> </strong> on 2009-08-30 04:59:41 <br /> @Sandro Franchi: Good catch... But shouldn't it be: <pre><code>if (precision !== undefined) {</code></pre> ? If it is as you had it, 0 will also be ignored. <hr /> <strong> <a href="http://alegua.com.ar" rel="nofollow">Sandro Franchi</a> </strong> on 2009-08-29 17:48:32 <br /> In Line 054 <pre><code> if (precision != null) {... </code></pre> should be changed to <pre><code> if (precision) {... </code></pre> because precision (when not used) is &quot;undefined&quot;, not null, also this helps to make the script validated by jslint. <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2009-01-08 17:03:36 <br /> @ ejsanders: Very well, thank you! <hr /> <strong> <a href="http://cantorion.org" rel="nofollow">ejsanders</a> </strong> on 2009-01-08 12:29:19 <br /> Here is the related function vsprintf, which takes the arguments as an array (can be useful): <pre><code> function vsprintf(format, args) { return sprintf.apply(this, [format].concat(args)); } </code></pre> <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2008-12-01 09:13:31 <br /> @ Paulo Ricardo F. Santos: Fixed it, thanks for pointing that out. <hr /> <strong> Paulo Ricardo F. Santos </strong> on 2008-11-28 11:53:19 <br /> Hey, the current implementation misses the custom padding character: <pre><code>printf(&amp;quot;[%'#10s]\n&amp;quot;, $s); // use the custom padding character '#'</code></pre> ;) <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2008-11-09 14:28:59 <br /> @ David Portabella: I did some testing and as far as I can tell PHP itself does not support that notation. And a general rule of thumb for us is: if php doesn't do it, we don't either. This ensures maximum compatibilty. If I'm in error on this, let me know. Otherwise, the function: number_format might be what you're looking for. <hr /> <strong> Mike </strong> on 2008-11-09 12:57:29 <br /> If you want to serialize strings with multibyte chars (special chars) you must replace this line: val = &quot;s:&quot; + mixed_value.length + &quot;:\&quot;&quot; + mixed_value + &quot;\&quot;&quot;; with these: var stringLen = encodeURIComponent(mixed_value).replace(/%../g, 'x').length val = &quot;s:&quot; + stringLen + &quot;:\&quot;&quot; + mixed_value + &quot;\&quot;&quot;; <hr /> <strong> David Portabella </strong> on 2008-11-04 20:03:40 <br /> Hello, Congratulations for this great library!! one question, Is it possible to specify a thousand separator for sprintf? something like sprintf('%,.2f&amp;quot;, 1234567.89) =&amp;gt; &amp;quot;1,234,567.89&amp;quot; Regards, DAvid <hr />