phpjs
Version:
85 lines (70 loc) • 2.47 kB
Markdown
layout: page
title: "JavaScript str_rot13 function"
comments: true
sharing: true
footer: true
alias:
- /functions/view/str_rot13:528
- /functions/view/str_rot13
- /functions/view/528
- /functions/str_rot13:528
- /functions/528
<!-- Generated by Rakefile:build -->
A JavaScript equivalent of PHP's str_rot13
{% codeblock strings/str_rot13.js lang:js https://raw.github.com/kvz/phpjs/master/functions/strings/str_rot13.js raw on github %}
function str_rot13 (str) {
// From: http://phpjs.org/functions
// + 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));
});
}
{% endcodeblock %}
- [Raw function on GitHub](https://github.com/kvz/phpjs/blob/master/functions/strings/str_rot13.js)
Please note that php.js uses JavaScript objects as substitutes for PHP arrays, they are
the closest match to this hashtable-like data structure.
Please also note that php.js offers community built functions and goes by the
[McDonald's Theory](https://medium.com/what-i-learned-building/9216e1c9da7d). We'll put online
functions that are far from perfect, in the hopes to spark better contributions.
Do you have one? Then please just:
- [Edit on GitHub](https://github.com/kvz/phpjs/edit/master/functions/strings/str_rot13.js)
### Example 1
This code
{% codeblock lang:js example %}
str_rot13('Kevin van Zonneveld');
{% endcodeblock %}
Should return
{% codeblock lang:js returns %}
'Xriva ina Mbaariryq'
{% endcodeblock %}
### Example 2
This code
{% codeblock lang:js example %}
str_rot13('Xriva ina Mbaariryq');
{% endcodeblock %}
Should return
{% codeblock lang:js returns %}
'Kevin van Zonneveld'
{% endcodeblock %}
### Example 3
This code
{% codeblock lang:js example %}
str_rot13(33);
{% endcodeblock %}
Should return
{% codeblock lang:js returns %}
'33'
{% endcodeblock %}
### Other PHP functions in the strings extension
{% render_partial _includes/custom/strings.html %}