UNPKG

phpjs

Version:

php.js offers community built php functions in javascript

616 lines (482 loc) 19.5 kB
<!-- Generated by Rakefile:build --> <strong> <a href="http://brett-zamir.me" rel="nofollow">Brett Zamir</a> </strong> on 2012-07-05 11:39:05 <br /> @Rajiva: In addition to my last question, what browser were you using? <hr /> <strong> <a href="http://brett-zamir.me" rel="nofollow">Brett Zamir</a> </strong> on 2012-07-05 11:37:22 <br /> @Rajiva: Can you put your long sequence somewhere so we can confirm? I am not able to replicate with decodeURIComponent() (which would be giving the URI error) even with a very large sequence: <pre><code> var arr = decodeURIComponent(new Array(101000).join('%20')); </code></pre> <hr /> <strong> <a href="http://an3m1.com/" rel="nofollow">????? ?????? ? ?????</a> </strong> on 2012-04-23 14:49:42 <br /> I wonder how you got so good. HaHa ! This is really a fascinating blog, lots of stuff that I can get into. One thing I just want to say is that your design is so perfect ! You certainly know how to get a girls attention ! I’m glad that you’re here. I feel like I’ve learned something new by being here <hr /> <strong> Ye Lin Htun </strong> on 2012-03-28 10:05:56 <br /> I reli appreciate your tutorial. <hr /> <strong> Rajiva </strong> on 2012-01-24 07:51:35 <br /> Yesterday I got same problem : malformed URI sequence. I had try all of solution that you're shown, but no one solved my problem. Than, I try another way that I can do for fix the problem. I solved the problem with cutting half of tag value (string) of my XML file with substr($string, 0, 3000). I think the browser &amp; program can't handle and processing large string. Based on my experience, the program can work properly while the string contain maximal 3000 character. Is anyone have same experience with me? <hr /> <strong> Dj </strong> on 2011-07-29 14:57:29 <br /> Instead of replace '+' with '%20', replace it with an space directly, then is not redundant and decodeURIComponent() will has less characters to proccess <hr /> <strong> <a href="http://www.kingsquare.nl/" rel="nofollow">Tim de Koning</a> </strong> on 2011-06-03 11:03:00 <br /> @Brett: done! :-) <hr /> <strong> <a href="http://brett-zamir.me" rel="nofollow">Brett Zamir</a> </strong> on 2011-06-02 04:53:04 <br /> @Tim: Send me a file with your exact code to my Yahoo address : brettz9 <hr /> <strong> <a href="http://www.kingqsuare.nl" rel="nofollow">Tim de Koning</a> </strong> on 2011-06-01 23:25:59 <br /> @Brett: Yes. I tested this this a Win XP and Windows 7 machine now in multiple browsers, all with the same result. <hr /> <strong> <a href="http://brett-zamir.me" rel="nofollow">Brett Zamir</a> </strong> on 2011-06-01 19:26:11 <br /> @Tim: Just to verify: You are using the latest version above? (essentially just a light wrapper for decodeURIComponent). And in your script you have nothing else besides this function and the alert with the call to urldecode? <hr /> <strong> <a href="http://www.kingsquare.nl/" rel="nofollow">Tim de Koning</a> </strong> on 2011-06-01 12:59:37 <br /> @Brett: I don't get it. I get the same error in Chrome 11.0.696.71 (URIError: URI malformed) and IE 9.0.8 too. Maybe it has something to do with my locale-settings... (Dutch, so pretty straightforward)? Anyway... The old version works perfectly and uses the same decodeURIComponent() in the end! <hr /> <strong> <a href="http://brett-zamir.me" rel="nofollow">Brett Zamir</a> </strong> on 2011-06-01 12:24:26 <br /> @Tim: Strange---I'm using FF 4.0.1 also and not getting an error. Character set shouldn't matter for JavaScript, but it does for PHP (i.e., %80 is not valid for UTF8 so wanted to compare apples to apples). <hr /> <strong> <a href="http://www.kingsquare.nl/" rel="nofollow">Tim de Koning</a> </strong> on 2011-06-01 10:17:31 <br /> @Brett: alert(urldecode('%E2%82%AC')) in Firefox 4.0.1 throws the same error: <pre><code> URIError: malformed URI sequence </code></pre> (I don't think this has anything to do with the character set, but i only pointed it out because '€' does not exist in latin1 / ISO-8859-1. So one should take that in consideration when running the mentioned test.) <hr /> <strong> <a href="http://brett-zamir.me" rel="nofollow">Brett Zamir</a> </strong> on 2011-05-31 18:01:17 <br /> @Tim: All sites should be in UTF-8 nowadays (recommended by HTML5 also). Try this: &lt;?php header('content-type:text/html;charset=utf8'); print urldecode('%E2%82%AC'); ?&gt; and alert(urldecode('%E2%82%AC')) <hr /> <strong> <a href="http://www.kingsquare.nl/" rel="nofollow">Tim de Koning</a> </strong> on 2011-05-31 17:23:57 <br /> <pre><code> urldecode('%80'); </code></pre> Result in PHP: <pre><code> € // in Windows-1252 character set </code></pre> Result in JavaScript: <pre><code> ERROR: malformed URI sequence </code></pre> Suggested fix: revert to old version of urldecode, @ https://github.com/kvz/phpjs/blob/e81fa731a23fc07e14793796a23c5b2ebd2bb262/functions/url/urldecode.js <hr /> <strong> anton </strong> on 2010-09-09 08:41:53 <br /> %27Musim+Depan%2C+Barca+Rekrut+Torres+%26amp%3B+Fabregas%27%3Cbr+%2F%3EAzulgrana+akan+mengeluarkan+dana+%A3100+juta.+ INPUT : %23%3Ca+href%3D%22%23%21%2Ftopics%2Fsports%22+%3Esports%3C%2Fa%3E+%23%3Ca+href%3D%22%23%21%2Ftopics%2Fentertainment%22+%3Eentertainment%3C%2Fa%3E+%23%3Ca+href%3D%22%23%21%2Ftopics%2Fnews%22+%3Enews%3C%2Fa%3E+%23%3Ca+href%3D%22%23%21%2Ftopics%2Fliverpool%22+%3Eliverpool%3C%2Fa%3E+%23%3Ca+href%3D%22%23%21%2Ftopics%2Fbarca%22+%3Ebarca%3C%2Fa%3E+%3Cbr+%2F%3E%3Ca+href%3D%22http%3A%2F%2Fbola.vivanews.com%2Fnews%2Fread%2F174352--musim-depan--barca-rekrut-torres---fabregas-%22+target%3D%22_blank%22%3Ehttp%3A%2F%2Fbola.vivanews.com%2Fnews%2Fread%2F174352--musim-depan--barca-rekrut-torres---fabregas-%3C%2Fa%3E <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2010-09-08 21:33:42 <br /> @ anton: Please provide the exact url you are passing to this function, as it looks like your input is broken. <hr /> <strong> anton </strong> on 2010-09-08 21:09:55 <br /> hey.... I find a bug Uncaught URIError: URI malformed <pre><code> ret = decodeURIComponent(ret); </code></pre> HELP ME.. <hr /> <strong> <a href="http://brett-zamir.me" rel="nofollow">Brett Zamir</a> </strong> on 2010-01-08 03:33:58 <br /> @David, Yes, thanks for reporting; it was just recently fixed (see the comments below), but the changes haven't yet made it to the site. FYI, the &quot;raw js source&quot; always has the very latest version (and in this case is the same as you suggested). <hr /> <strong> David </strong> on 2010-01-07 22:54:22 <br /> The current version works pretty well, but need of one patch to &quot;space bug&quot;: <pre><code> // Current return decodeURIComponent(str).replace(/\+/g, '%20'); // Correct return decodeURIComponent(str.replace(/\+/g, '%20')); </code></pre> Bye. <hr /> <strong> <a href="http://brett-zamir.me" rel="nofollow">Brett Zamir</a> </strong> on 2010-01-01 07:56:33 <br /> Argh--Embarrassing mistake. Thanks for the fix. Done in Git... <hr /> <strong> Rob </strong> on 2010-01-01 04:42:45 <br /> I think: <pre><code>return decodeURIComponent(str).replace(/\+/g, '%20');</code></pre> should be: <pre><code>return decodeURIComponent(str.replace(/\+/g, '%20'));</code></pre> <hr /> <strong> <a href="http://brett-zamir.me" rel="nofollow">Brett Zamir</a> </strong> on 2009-10-29 07:05:02 <br /> @ajo: Yes, 100% free as in free donuts (I don't drink alcohol)... :) You only need to add a copy of the license at http://phpjs.org/pages/license with any code you redistribute for others. <hr /> <strong> ajo </strong> on 2009-10-29 03:04:53 <br /> i love it...i just love it... but is it 100% free to use? <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2009-09-04 19:03:29 <br /> @ Shamun Toha: If I'm not mistaken Brett is working on a different method so that we don't have to work work with hash_maps anymore. <hr /> <strong> Shamun Toha </strong> on 2009-08-30 12:26:04 <br /> <pre><code> function urldecode( str ) { // Decodes URL-encoded string // // version: 907.503 // discuss at: http://phpjs.org/functions/urldecode // + original by: Philip Peterson // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) // + input by: AJ // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) // + improved by: Brett Zamir (http://brett-zamir.me) // + input by: travc // + input by: Brett Zamir (http://brett-zamir.me) // + bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) // + improved by: Lars Fischer // + input by: Ratheous // + improved by: @Shamun Toha // + note by: - Shamun: decodeURIComponent failed on many characters // % note 1: info on what encoding functions to use from: http://xkr.us/articles/javascript/encode-compare/ // * example 1: urldecode('Kevin+van+Zonneveld%21'); // * returns 1: 'Kevin van Zonneveld!' // * example 2: urldecode('http%3A%2F%2Fkevin.vanzonneveld.net%2F'); // * returns 2: 'http://kevin.vanzonneveld.net/' // * example 3: urldecode('http%3A%2F%2Fwww.google.nl%2Fsearch%3Fq%3Dphp.js%26ie%3Dutf-8%26oe%3Dutf-8%26aq%3Dt%26rls%3Dcom.ubuntu%3Aen-US%3Aunofficial%26client%3Dfirefox-a'); // * returns 3: 'http://www.google.nl/search?q=php.js&amp;ie=utf-8&amp;oe=utf-8&amp;aq=t&amp;rls=com.ubuntu:en-US:unofficial&amp;client=firefox-a' // * example 4: urldecode(&quot;%3Ctr%3E%3Ctd%3Einfo%40jquery.com%20space%20%E9%3C%2Ftd%3E%3C%2Ftr%3E&quot;); // * returns 4: &lt;tr&gt;&lt;td&gt;info@jquery.com Space e&lt;/td&gt;&lt;/tr&gt; var hash_map = {}, ret = str.toString(), unicodeStr='', hexEscStr=''; var replacer = function(search, replace, str) { var tmp_arr = []; tmp_arr = str.split(search); return tmp_arr.join(replace); }; // The hash_map is identical to the one in urlencode. hash_map[&quot;'&quot;] = '%27'; hash_map['('] = '%28'; hash_map[')'] = '%29'; hash_map['*'] = '%2A'; hash_map['~'] = '%7E'; hash_map['!'] = '%21'; hash_map['%20'] = '+'; hash_map['\u00DC'] = '%DC'; hash_map['\u00FC'] = '%FC'; hash_map['\u00C4'] = '%D4'; hash_map['\u00E4'] = '%E4'; hash_map['\u00D6'] = '%D6'; hash_map['\u00F6'] = '%F6'; hash_map['\u00DF'] = '%DF'; hash_map['\u20AC'] = '%80'; hash_map['\u0081'] = '%81'; hash_map['\u201A'] = '%82'; hash_map['\u0192'] = '%83'; hash_map['\u201E'] = '%84'; hash_map['\u2026'] = '%85'; hash_map['\u2020'] = '%86'; hash_map['\u2021'] = '%87'; hash_map['\u02C6'] = '%88'; hash_map['\u2030'] = '%89'; hash_map['\u0160'] = '%8A'; hash_map['\u2039'] = '%8B'; hash_map['\u0152'] = '%8C'; hash_map['\u008D'] = '%8D'; hash_map['\u017D'] = '%8E'; hash_map['\u008F'] = '%8F'; hash_map['\u0090'] = '%90'; hash_map['\u2018'] = '%91'; hash_map['\u2019'] = '%92'; hash_map['\u201C'] = '%93'; hash_map['\u201D'] = '%94'; hash_map['\u2022'] = '%95'; hash_map['\u2013'] = '%96'; hash_map['\u2014'] = '%97'; hash_map['\u02DC'] = '%98'; hash_map['\u2122'] = '%99'; hash_map['\u0161'] = '%9A'; hash_map['\u203A'] = '%9B'; hash_map['\u0153'] = '%9C'; hash_map['\u009D'] = '%9D'; hash_map['\u017E'] = '%9E'; hash_map['\u0178'] = '%9F'; // on decodeURIComponent failure. hash_map['&lt;'] = '%3C'; hash_map['&gt;'] = '%3E'; hash_map['/'] = '%2F'; hash_map['@'] = '%40'; hash_map['e'] = '%E9'; hash_map[' '] = '%20'; for (unicodeStr in hash_map) { hexEscStr = hash_map[unicodeStr]; // Switch order when decoding ret = replacer(hexEscStr, unicodeStr, ret); // Custom replace. No regexing } // End with decodeURIComponent, which most resembles PHP's encoding functions ret = decodeURIComponent(ret); return ret; } </code></pre> Test suite: <pre><code> // output this: &lt;tr&gt;&lt;td&gt;info@jquery.com Space e&lt;/td&gt;&lt;/tr&gt; var a = &quot;%3Ctr%3E%3Ctd%3Einfo%40jquery.com%20space%20%E9%3C%2Ftd%3E%3C%2Ftr%3E&quot;; alert ( urldecode(a) ); </code></pre> <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2009-08-22 17:14:42 <br /> @ Brett Zamir: You are the man! :D Hands down! <hr /> <strong> <a href="http://brett-zamir.me" rel="nofollow">Brett Zamir</a> </strong> on 2009-08-18 12:10:34 <br /> @CDuv: The escape sequence, &quot;%E7a%20plante&quot; gives an invalid character in PHP as well. Are you using UTF-8 like a good netizen? :) The urlencode() (in PHP or JavaScript) used on &quot;ça plante&quot; should be giving: &quot;%C3%A7a+plante&quot; which can then be decoded by either as well. This function needs to drop some of the code that was mistakenly added for the sake of ISO Latin--which is not used in PHP or JS, from my testing. <hr /> <strong> <a href="http://claude.duvergier.fr" rel="nofollow">CDuv</a> </strong> on 2009-08-17 17:35:37 <br /> I had a &quot;malformed URI sequence&quot; JavaScript error in Firefox/3.5.2 (Gecko/20090729) error console when trying to url-decode : &quot;%E7a%20plante&quot; (&quot;ça plante&quot;). I read on OpenLayers bugtracker Ticket #1704 (http://trac.openlayers.org/ticket/1704) that JavaScript unescape() function could help. So I gave it a chance and changed<pre><code>ret = decodeURIComponent(ret);</code></pre>(line 90 in phpjs.org::urldecode() v908.1617) To<pre><code>ret = decodeURIComponent(unescape(ret));</code></pre> As it solved by problem I would like to share it with you :) <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2009-08-16 15:31:34 <br /> @ Brett Zamir: Agreed. until we do though, I'd like to add Orlando's improvements to the hash_map @ Orlando: Thanks for sharing, fixed in SVN <hr /> <strong> <a href="http://brett-zamir.me" rel="nofollow">Brett Zamir</a> </strong> on 2009-08-07 05:59:40 <br /> @Orlando, thanks for pointing this out. However, if someone has some time now, could we look into solving this problem (that keeps coming up) of seeing non-ASCII characters translated correctly for all languages? There must be some algorithm we can use rather than just pasting new translations piecemeal... <hr /> <strong> Orlando </strong> on 2009-08-07 00:15:49 <br /> I was working with norwegian characters but wasn't able to get it working for uppercase characters(Ø,Å,Æ) until I added the following lines to hashmap: <pre><code> hash_map['\u00C6'] = '%C3%86'; hash_map['\u00D8'] = '%C3%98'; hash_map['\u00C5'] = '%C3%85'; </code></pre> I got those values when encoding at php with urlencode(). <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2009-04-14 12:11:58 <br /> @ Lars Fischer: Thanks for helping us make it better! <hr /> <strong> Lars Fischer </strong> on 2009-04-07 16:20:38 <br /> This is a wonderful Project! I'm glad, finding it today! Really good work! For the german special-chars, this should be added to the urlencode and urldecode-function: <pre><code> histogram['\u00DC'] = '%DC'; histogram['\u00FC'] = '%FC'; histogram['\u00C4'] = '%D4'; histogram['\u00E4'] = '%E4'; histogram['\u00D6'] = '%D6'; histogram['\u00F6'] = '%F6'; </code></pre> histogram['\u00DF'] = '%DF'; <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2009-01-14 11:06:32 <br /> @ Brett Zamir: Tired? maybe. Of you? Never :) Keep 'em coming! :D <hr /> <strong> <a href="http://bahai-library.com" rel="nofollow">Brett Zamir</a> </strong> on 2009-01-13 19:00:30 <br /> Hi there...Getting tired of me yet? The following should be added to this function and for urlencode(): <pre><code> histogram[&amp;quot;'&amp;quot;] = '%27'; histogram['('] = '%28'; histogram[')'] = '%29'; histogram['*'] = '%2A'; histogram['~'] = '%7E'; </code></pre> And once you fix the above, you can also get rawurlencode and rawurldecode by simply removing the following line from each of the corresponding files: <pre><code>histogram['%20'] = '+';</code></pre> (That's the only difference with the raw form) <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2009-01-08 16:58:49 <br /> @ Guilherme: Sorry your comment got stuck in the spamfilter. Akismet is a bit strict sometimes (can't really blame it, there are a lot of unrecognized words in your comment). Anyway: You are not supposed to use php tags. This should prove to be more successful: <pre><code> var $__t = '\'a\' +b+ \&amp;quot;c\&amp;quot; %d% \\\\ \\\\'; alert(urlencode($__t)); </code></pre> <hr /> <strong> Guilherme </strong> on 2008-12-31 03:59:29 <br /> Hi, I guess your function doesn't work for some situations. Try this code (I'll appreciate if you send me the fixes): <pre><code> &amp;lt;?php $__t = '\'a\' +b+ \&quot;c\&quot; %d% \\\\ \\\\'; ?&amp;gt; alert(&quot;&amp;lt;?= $__t ?&amp;gt;&quot;); alert(&quot;&amp;lt;?= urlencode($__t) ?&amp;gt;&quot;); alert(&quot;&amp;lt;?= urldecode(urlencode($__t)) ?&amp;gt;&quot;); alert(urldecode(&quot;&amp;lt;?= urlencode($__t) ?&amp;gt;&quot;)); // It fails with your function! </code></pre> <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2008-08-27 19:31:51 <br /> @ johnrembo: Hi John, thanks for your input again. We had some discussion about it earlier. It doesn't mimic PHP behavior enough. Differences between JavaScript's encoding functionalities can be found here: http://xkr.us/articles/javascript/encode-compare/ <hr /> <strong> johnrembo </strong> on 2008-08-27 17:08:31 <br /> <pre><code> function urdencode (str) { return decodeURIComponent(str); } </code></pre> <hr /> <strong> thor s. </strong> on 2008-07-20 23:18:25 <br /> thank you a million times!!!! <hr /> <strong> larry fisher </strong> on 2008-07-11 15:29:45 <br /> just want to thank you. very useful... <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2008-06-16 00:14:15 <br /> @ doqkhanh: Why do we really need this handling? Because I don't believe PHP would return such an error string, and we would like to mimic it's behavior as strict as possible, to avoid strange bugs &amp;amp; unexpected results; and so that the PHP documentation doesn't need to be changed. <hr /> <strong> doqkhanh </strong> on 2008-06-13 03:26:22 <br /> I have a safer function: /** * \summary: http://kevin.vanzonneveld.net * \author: Original by: Philip Peterson * improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) * \example: urlencode('Kevin van Zonneveld!'); * \returns: 'Kevin+van+Zonneveld%21' */ function urlencode( str ) { var errorMessage = &amp;quot;Javascript error at urlencode() function in yourfile.js. Please contact YourCompany's site administrator.&amp;quot;; var ret = str; try { ret = ret.toString(); ret = encodeURIComponent(ret); ret = ret.replace(/%20/g, '+'); } catch(err) { alert(errorMessage); } return ret; } <hr />