Converts the characters "&", "<", ">", '"' (double quote), and "'" (apostrophe), in a string to their corresponding HTML entities.
A script by V.
function convertHTMLent(str) { | |
// :) | |
var replace = ["&","<",">"]; | |
replace.push("'"); | |
replace.push('"'); | |
var replacements = ["&","<",">","'","""]; | |
var counter = 0; | |
// count entries to replace | |
for (var i=0;i<replace.length;i++){ | |
for (var j=0;j<str.length;j++){ | |
if (replace[i] == str[j]){ | |
counter = counter + 1; | |
} | |
} | |
} | |
for (var k=0;k<counter;k++){ | |
if (str.indexOf("& ") > -1){ | |
str = str.replace(/&/,replacements[0]); | |
} | |
if (str.indexOf("<") > -1){ | |
str = str.replace(/</,replacements[1]); | |
} | |
if (str.indexOf(">") > -1){ | |
str = str.replace(/>/,replacements[2]); | |
} | |
if (str.indexOf("'") > -1){ | |
str = str.replace(/'/,replacements[3]); | |
} | |
if (str.indexOf('"') > -1){ | |
str = str.replace(/"/,replacements[4]); | |
} | |
} | |
return str; | |
} | |
convertHTMLent('Stuff in "quotation marks"'); |