My htmlspecialchars() functions for Javascript
I thought I'd post my htmlspecialchars() functions for Javascript. This should mimic the PHP version of htmlspecialchars(). I also include rhtmlspecialchars() in case you need to do the reverse.
function htmlspecialchars(str) {
if (typeof(str) == "string") {
str = str.replace(/&/g, "&"); /* must do & first */
str = str.replace(/"/g, """);
str = str.replace(/'/g, "'");
str = str.replace(/</g, "<");
str = str.replace(/>/g, ">");
}
return str;
} |
function htmlspecialchars(str) {
if (typeof(str) == "string") {
str = str.replace(/&/g, "&"); /* must do & first */
str = str.replace(/"/g, """);
str = str.replace(/'/g, "'");
str = str.replace(/</g, "<");
str = str.replace(/>/g, ">");
}
return str;
}
function rhtmlspecialchars(str) {
if (typeof(str) == "string") {
str = str.replace(/>/ig, ">");
str = str.replace(/</ig, "<");
str = str.replace(/'/g, "'");
str = str.replace(/"/ig, '"');
str = str.replace(/&/ig, '&'); /* must do & last */
}
return str;
} |
function rhtmlspecialchars(str) {
if (typeof(str) == "string") {
str = str.replace(/>/ig, ">");
str = str.replace(/</ig, "<");
str = str.replace(/'/g, "'");
str = str.replace(/"/ig, '"');
str = str.replace(/&/ig, '&'); /* must do & last */
}
return str;
}
you rock, works great!
Excellent job. Thank you.
Thanks you sooooo much. Works great. 🙂