Use PHP functions in JavaScript

JavaScript str_rot13

Perform the rot13 transform on a string

1
2
3
4
56
7
8
9
1011
12
13
14
1516
17
18
19
20
function str_rot13 (str) {
    // Perform the rot13 transform on a string  
    // 
    // version: 1008.1718
    // discuss at: http://phpjs.org/functions/str_rot13    // +   original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +   improved by: Ates Goral (http://magnetiq.com)
    // +   bugfixed by: Onno Marsman
    // +   improved by: Rafał Kukawski (http://blog.kukawski.pl)
    // *     example 1: str_rot13('Kevin van Zonneveld');    // *     returns 1: 'Xriva ina Mbaariryq'
    // *     example 2: str_rot13('Xriva ina Mbaariryq');
    // *     returns 2: 'Kevin van Zonneveld'
    // *     example 3: str_rot13(33);
    // *     returns 3: '33'    
    return (str+'').replace(/[a-z]/gi, function(s){
        return String.fromCharCode(s.charCodeAt(0)+(s.toLowerCase()<'n'?13:-13));
    });
}
external links: original PHP docs | raw js source

Examples

» Example 1

Running

1
str_rot13('Kevin van Zonneveld');

Should return

1
'Xriva ina Mbaariryq'

» Example 2

Running

1
str_rot13('Xriva ina Mbaariryq');

Should return

1
'Kevin van Zonneveld'

Dependencies

No dependencies, you can use this function standalone.

Open syntax issues

php.js uses JsLint to help us keep our code consistent and prevent some common bugs.

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.


Authors

Thanks to the following developers, you get to have str_rot13 goodness in JavaScript.

Comments

Add Comment
Use:
[CODE]
your_stuff('here');
[/CODE]
for proper code formatting
By submitting code here you are allowing us to use it in php.js hence dual licensing it under the MIT and GPL licenses

Gravatar
Kevin van Zonneveld
Apr 23rd Permalink

q  @ Rafał Kukawski: It is, thanks!
http://github.com/kvz/phpjs/commit/0341548f49543d24a4f33f3261f5d844c3e12bbb

Gravatar
Rafał Kukawski
Apr 23rd Permalink

q  I forgot to cast the input value to string, please change

str.replace(/[a-z]/gi 


to

(str + '').replace(/[a-z]/gi


I think it's important.

Gravatar
Kevin van Zonneveld
Apr 23rd Permalink

q  @ Rafał Kukawski: Excellent work! Tests pass, we fixes jslint issue, and and function is tighter. Thanks a lot!
http://github.com/kvz/phpjs/commit/f4fcf895c16ecce83550efc9c190c82ed118c06c

Gravatar
Kevin van Zonneveld
Apr 23rd Permalink

q  @ Rafał Kukawski: Excellent work! Tests pass, we fixes jslint issue, and and function is tighter. Thanks a lot!
http://github.com/kvz/phpjs/commit/f4fcf895c16ecce83550efc9c190c82ed118c06c

Gravatar
Rafał Kukawski
Apr 23rd Permalink

q  My proposition (uses the conditional operator)

function rot13(x){ 
   return x.replace(/[a-z]/gi,function(s){ 
      return String.fromCharCode(s.charCodeAt(0)+(s.toLowerCase()<'n'?13:-13))}) 
}

Gravatar
Kevin van Zonneveld
4 Aug '09 Permalink

q  @ Eric & Brett Zamir: Both sites (php.js & my blog) have a pagerank of 5 currently. I guess it's just a matter of time for PHP.JS to receive more backlinks from other sites & get a higher ranking for people who want to find our javascript snippets.

Gravatar
Brett Zamir
30 Jul '09 Permalink

q  Not sure why it didn't make it, though at least str_rot13 with or without JavaScript made it to the first page. Thanks to your prompting, I went ahead and did a little further SEO investigation of the site and sent the recommendations to Kevin (though he's already done a fine job of bringing his site to sometimes even rival the PHP site when you search for PHP functions)

Gravatar
Eric
30 Jul '09 Permalink

q  Great!, only like 3 lines of code, I like this alot and its just what I'm looking for. Why on earth is this page not listed on Google for #1 spot of "rot13 javascript"?, on Google all I could find where old rot13 implementations that has like 40 lines of code written years ago using schemes for javascript 1.0. Glad I found this website! Thanks alot.

Gravatar
Kevin van Zonneveld
22 Jan '08 Permalink

q  @ Ates Goral: I agree, it's purdy ;)

Gravatar
Ates Goral
22 Jan '08 Permalink

q  Alternative implementation that doesn't use conditionals:

[CODE=&quot;Javascript&quot;]
function str_rot13(str) {
// * example 1: str_rot13(&quot;Hello World!&quot;);
// * returns 1: &quot;Uryyb Jbeyq!&quot;
// * example 2: str_rot13(str_rot13(&quot;Hello World!&quot;));
// * returns 2: &quot;Hello World!&quot;

return str.replace(/[A-Za-z]/g, function (c) { return String.fromCharCode((((c = c.charCodeAt(0)) &amp; 223) - 52) % 26 + (c &amp; 32) + 65); });
}
[/CODE]


Contribute a New function