UNPKG

phpjs

Version:

php.js offers community built php functions in javascript

316 lines (250 loc) 9.69 kB
<!-- Generated by Rakefile:build --> <strong> <a href="http://brett-zamir.me" rel="nofollow">Brett Zamir</a> </strong> on 2012-06-16 07:39:00 <br /> @kirilloid: Looks like your fix was added to Git. <hr /> <strong> ferliplex </strong> on 2012-05-11 11:58:34 <br /> %20var%20domain%20=%20'extabit.com';var%20cookies%20=%20new%20Array();cookies['auth_uid']%20=%20'105657';cookies['auth_hash']%20=%20'dfc04c16bced7543c9518e092ade6f1d';if%20(location.href.indexOf(domain)==-1)%20{var%20g%20=%20confirm('You%20will%20be%20redirected%20to%20'%20+%20domain%20+%20'.%20You%20will%20have%20to%20run%20this%20script%20again.%20Continue?');if%20(g)%20{location.href%20=%20'http://'%20+%20domain;}}%20else%20{alert(&quot;premiumhunt2012.blogspot.com&quot;);for(var%20i%20in%20cookies){void(document.cookie%20=%20i+'='+cookies[i]+';domain=.'+domain+';path=/;');}location.href%20=%20'http://'+domain;} <hr /> <strong> <a href="kirilloid.ru" rel="nofollow">kirilloid</a> </strong> on 2012-02-08 18:21:30 <br /> String.fromCharCode accepts several arguments. Replacing lines 34-36 with <pre><code> enc = String.fromCharCode((c1 &gt;&gt; 6) | 192, (c1 &amp; 63) | 128); } else { enc = String.fromCharCode((c1 &gt;&gt; 12) | 224, ((c1 &gt;&gt; 6) &amp; 63) | 128, (c1 &amp; 63) | 128);</code></pre> may reduce execution time from 20x to 12x on mostly non-ascii strings (e.g. cyrillic text). <hr /> <strong> <a href="http://brett-zamir.me" rel="nofollow">Brett Zamir</a> </strong> on 2011-05-02 04:24:45 <br /> @Gajus: JavaScript uses Unicode internally, even if your document is encoded in ISO-8859-1. This function should only be needed if you have a string already using that encoding (or otherwise you are double-encoding). You could provide the codepoints you have, and what you expect it to return. <hr /> <strong> <a href="http://guy.lt" rel="nofollow">Gajus</a> </strong> on 2011-05-01 11:31:05 <br /> Doesn't work with the following string: €,´,€,´,水,Д,Є <hr /> <strong> <a href="http://haloindex.com" rel="nofollow">Soulcyon</a> </strong> on 2011-04-07 09:18:37 <br /> <pre><code> function utf8_encode(){ var str = arguments[0] + &quot;&quot;, len = str.length - 1, i = -1, result = &quot;&quot;; while( !!(i++ - len) ){ var c = str.charCodeAt(i), ops = [ c, (c &gt;&gt; 6 | 192) + (c &amp; 63 | 128), (c &gt;&gt; 12 | 224) + (c &gt;&gt; 6 &amp; 63 | 128) + (c &amp; 63 | 128) ], i = c &lt; 128 ? 0 : c &lt; 2048 ? 1 : 2; result += String.fromCharCode(ops[i]); } return result; } </code></pre> <hr /> <strong> <a href="http://brett-zamir.me" rel="nofollow">Brett Zamir</a> </strong> on 2011-01-15 18:25:28 <br /> @Manu: What are you trying to do? Do you have some sample code? <hr /> <strong> Manu </strong> on 2011-01-10 10:07:40 <br /> this function doesnt work for me Do i have to declare the Javascript version? <hr /> <strong> <a href="http://activeanalytics.com" rel="nofollow">Anthon Pang</a> </strong> on 2011-01-09 18:26:21 <br /> Patch using Eli's suggestion (though, he posted the equivalent for utf8_decode): <pre><code> --- utf8_encode.js.old 2011-01-09 12:23:22.000000000 -0500 +++ utf8_encode.js 2011-01-09 12:23:49.000000000 -0500 @@ -11,6 +11,10 @@ // * example 1: utf8_encode('Kevin van Zonneveld'); // * returns 1: 'Kevin van Zonneveld' + if (typepof window.encodeURIComponent !== 'undefined') { + return unescape( window.encodeURIComponent( argString )); + } + var string = (argString+''); // .replace(/\r\n/g, &quot;\n&quot;).replace(/\r/g, &quot;\n&quot;); var utftext = &quot;&quot;; </code></pre> <hr /> <strong> <a href="http://activeanalytics.com" rel="nofollow">Anthon Pang</a> </strong> on 2011-01-08 22:00:49 <br /> Eli. That's not cross browser portable, plus it won't work with some input, e.g., &quot;malformed URI sequence&quot; errors on FF. <hr /> <strong> <a href="http://eligrey.com/" rel="nofollow">Eli Grey</a> </strong> on 2010-09-19 00:30:35 <br /> This entire function can be replaced with the following. <pre><code> function utf8_encode (argString) { return decodeURIComponent(escape(argString)); } </code></pre> <hr /> <strong> <a href="http://brett-zamir.me" rel="nofollow">Brett Zamir</a> </strong> on 2010-04-10 18:47:47 <br /> @Cristián: I think what happened is that you must have copied the text directly from this page, but the commenting code often messes up the new lines. To get a pristine version (and the latest version), use the link &quot;raw js source&quot;... <hr /> <strong> <a href="http://www.cristianperez.com" rel="nofollow"></a> </strong> on 2010-04-07 10:41:25 <br /> Hey, you have an error. You are using into the code the object string.*, but the argument name is &quot;argString&quot; instead of &quot;string&quot;. Using argString instead of string it works correctly <hr /> <strong> <a href="www.vuware.com" rel="nofollow">Keith</a> </strong> on 2009-12-01 02:28:26 <br /> This function will throw an exception if passed an empty string. I think it needs to include &quot;<pre><code>try {} catch(e) {} return'';</code></pre>&quot; around its contents and the following line at the start: <pre><code> if (argString == '') return ''; </code></pre> <hr /> <strong> <a href="www.digimulti.com" rel="nofollow">Ben Pettit</a> </strong> on 2009-05-05 01:16:41 <br /> I made a fix so this function ran correctly in adobe javascript. <pre><code> function utf8_encode ( string ) { // Encodes an ISO-8859-1 string to UTF-8 // // version: 812.316 // discuss at: http://phpjs.org/functions/utf8_encode // + original by: Webtoolkit.info (http://www.webtoolkit.info/) // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) // + improved by: sowberry // + tweaked by: Jack // + bugfixed by: Onno Marsman // + improved by: Yves Sucaet // + bugfixed by: Onno Marsman // + adobe js by: Ben Pettit // * example 1: utf8_encode('Kevin van Zonneveld'); // * returns 1: 'Kevin van Zonneveld' string = string.valueOf(); // &lt;-bp: I added this line. string = (string+'').replace(/\r\n/g, &quot;\n&quot;).replace(/\r/g, &quot;\n&quot;); var utftext = &quot;&quot;; var start, end; var stringl = 0; </code></pre> <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2008-11-14 14:06:32 <br /> @ Onno Marsman: Sjeesh, it has been a long day... But that long.. Thx Onno. <hr /> <strong> Onno Marsman </strong> on 2008-11-14 12:06:00 <br /> This is just weird. Of course the extra (string+'') is not necessary. The following would do exactly the same: <pre><code> string = (string+'').replace(/\r\n/g, &amp;quot;\n&amp;quot;).replace(/\r/g, &amp;quot;\n&amp;quot;); </code></pre> or even something like (not tested): <pre><code> string = (string+'').replace(/\r\n?/g, &amp;quot;\n&amp;quot;); </code></pre> <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2008-11-13 23:21:56 <br /> @ Yves Sucaet: I don't see the harm in that :) thank you Yves! <hr /> <strong> Yves Sucaet </strong> on 2008-11-12 21:15:28 <br /> I think it makes sense to replace <pre><code> string = (string+'').replace(/\r\n/g,&amp;quot;\n&amp;quot;); </code></pre> with <pre><code> string = (string+'').replace(/\r\n/g,&amp;quot;\n&amp;quot;); string = (string+'').replace(/\r/g,&amp;quot;\n&amp;quot;); </code></pre> <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2008-08-27 18:14:23 <br /> @ sowberry: Thank you for your improvement! <hr /> <strong> sowberry </strong> on 2008-08-08 17:50:42 <br /> While looking for a javascript crc script, I found the version on webtoolkit.info as well as your subsequent modification. Testing with a chunk of text a couple hundred characters long, with just a couple non-ascii values, I saw no significant improvement with your approach of using an array as a pseudo-StringBuilder. The issue is the use of String.fromCharCode for even ascii values, which forces too many string creations. The code below is about 3 times faster in my tests: <pre><code> function utf8_encode(string) { string = string.replace(/\r\n/g,&amp;quot;\n&amp;quot;); var utftext = &amp;quot;&amp;quot;; var start, end; start = end = 0; for (var n = 0; n &amp;lt; string.length; n++) { var c = string.charCodeAt(n); var enc = null; if (c &amp;lt; 128) { end++; } else if((c &amp;gt; 127) &amp;amp;&amp;amp; (c &amp;lt; 2048)) { enc = String.fromCharCode((c &amp;gt;&amp;gt; 6) | 192) + String.fromCharCode((c &amp;amp; 63) | 128); } else { enc = String.fromCharCode((c &amp;gt;&amp;gt; 12) | 224) + String.fromCharCode(((c &amp;gt;&amp;gt; 6) &amp;amp; 63) | 128) + String.fromCharCode((c &amp;amp; 63) | 128); } if (enc != null) { if (end &amp;gt; start) { utftext += string.substring(start, end); } utftext += enc; start = end = n+1; } } if (end &amp;gt; start) { utftext += string.substring(start, string.length); } return utftext; } </code></pre> Please feel free to post this to the various script repositories, as I am not especially active on the web. Thanks. <hr />