ColdFusion UDF Library - htmlEscape

December 16th, 2008
«« Back to ColdFusion Library

Category:
   STRINGS

Description:
   escapes html

Parameters:
  1. str (String, required)
           The string containing the HTML you wish to escape.


Returns:
       

Requires:
       (String) escaped html

Sample Use:
       escapeMe = htmlEscape("foobar");

Copy to Clipboard
function htmlEscape(str){
	var loop = 0;
	var strLen = len(str);
	var ascii = 0;
	var hex = "";
	var codes = "34,38,60,62,338,339,352,353,376,402,710,732,977,978,982,8194,8195,8201,8211,8212,8230,8240,8242,8243,8249,8250,8254,8260,8364,8465,8472,8476,8482,8501,8629,8706,8707,8709,8715,8719,8721,8722,8727,8730,8733,8734,8736,8756,8764,8773,8776,8800,8801,8804,8805,8835,8836,8838,8839,8853,8855,8869,8901,9001,9002,9674,9824,9827,9829,9830";
	for (loop = 1; loop lte len(str); loop = incrementValue(loop)){
		ascii = asc(mid(str,loop,1));
		if (listFindNoCase(codes,ascii) or (ascii gte 160 and ascii lte 255) or (ascii gte 913 and ascii lte 929) or (ascii gte 931 and ascii lte 937) or (ascii gte 945 and ascii lte 969) or (ascii gte 8204 and ascii lte 8207) or (ascii gte 8216 and ascii lte 8218) or (ascii gte 8220 and ascii lte 8222) or (ascii gte 8224 and ascii lte 8226) or (ascii gte 8292 and ascii lte 8296) or (ascii gte 8659 and ascii lte 8660) or (ascii gte 8711 and ascii lte 8713) or (ascii gte 8743 and ascii lte 8747) or (ascii gte 8968 and ascii lte 8971)){
			hex = formatBaseN(ascii,16);
			if (len(hex) lt 4)hex = "00"&hex;
			str = insert("&##x#hex#;",str,loop);
			str = removeChars(str,loop,1);
			loop = loop+7;
		};
	};
	return str;
};
// http://www.programmers.org/index.php/coldfusion-udf-library/?ckey=74&function=htmlEscape
  1. No comments yet.
  1. No trackbacks yet.