在水晶报表中将非字母数字字符替换为html

时间:2015-07-28 09:27:01

标签: html crystal-reports ascii

我正在构建一个水晶报告,为第三方应用程序输出XML格式的文本。

第三方应用无法处理非字母数字字符,因此我必须将它们转换为HTML才能处理。目前,我使用此代码来捕获最常见的字符:

stringvar output := {table.fieldName};
output := Trim(output);  
output := Replace (output,chrW(38),"&");
output := Replace (output,chrW(59),"&#59;");
output := Replace (output,"!","!");
output := Replace (output,chr(34),""");
output := Replace (output,chrW(35),"#");
output := Replace (output,"$","$");
output := Replace (output,"%","%");
output := Replace (output, chrW(39),"'");
output := Replace (output,"(","(");
output := Replace (output,")",")");
output := Replace (output,"*","*");
output := Replace (output,"+","+");
output := Replace (output,",",",");
output := Replace (output,"-","-");
output := Replace (output,".",".");
output := Replace (output,"/","/");
output := Replace (output,":",":");
output := Replace (output,"<","&#60;");
output := Replace (output,"=","&#61;");
output := Replace (output,">","&#62;");
output := Replace (output,"?","&#63;");
output := Replace (output,"@","&#64;");
output := Replace (output,"[","&#91;");
output := Replace (output,"\","&#92;");
output := Replace (output,"]","&#93;");
output := Replace (output,"^","&#94;");
output := Replace (output,"_","&#95;");
output := Replace (output,"`","&#96;");
output := Replace (output,"{","&#123;");
output := Replace (output,"|","&#124;");
output := Replace (output,"}","&#125;");
output := Replace (output,"~","&#126;");
output := Replace (output, chrW(145),"&#145;");
output := Replace (output, chrW(146),"&#146;");
output := Replace (output, chrW(147),"&#147;");
output := Replace (output, chrW(148),"&#148;");
output := Replace (output, chrW(8212),"&#8212;");
output := Replace (output, chrW(8217),"&#8217;");
output := Replace (output, chrW(8220),"&#8220;");
output := Replace (output, chrW(8221),"&#8221;");
output := Replace (output,"£","&#156;");

它不健康,需要维护。我在找到它们时添加了字符,但是我想知道是否有可能使用AscW和ChrW来动态识别和转换非字母数字字符的方法。字符串中的html / ascii等价物。

1 个答案:

答案 0 :(得分:1)

我似乎通过调整用于删除非字母数字字符的代码找到了此解决方案:

stringvar input := {table.fieldName};
stringvar output := '';
numbervar i;

input := Trim(input);

for i := 1 to Length(input) Step 1 do

  // 0-9 is 48-57
  // A-Z is 65-90
  // a-z is 97-122

  if not(input[i] in [chr(32), Chr(48),Chr(49),Chr(50),Chr(51),Chr(52),Chr(53),Chr(54),Chr(55),Chr(56),Chr(57),Chr(65),Chr(66),Chr(67),Chr(68),Chr(69),Chr(70),Chr(71),Chr(72),Chr(73),Chr(74),Chr(75),Chr(76),Chr(77),Chr(78),Chr(79),Chr(80),Chr(81),Chr(82),Chr(83),Chr(84),Chr(85),Chr(86),Chr(87),Chr(88),Chr(89),Chr(90),Chr(97),Chr(98),Chr(99),Chr(100),Chr(101),Chr(102),Chr(103),Chr(104),Chr(105),Chr(106),Chr(107),Chr(108),Chr(109),Chr(110),Chr(111),Chr(112),Chr(113),Chr(114),Chr(115),Chr(116),Chr(117),Chr(118),Chr(119),Chr(120),Chr(121),Chr(122)]) 
  then 

(output := output + "&#"+ cstr(ascw(input[i]),0)+";";)

else

  if (input[i] in [chr(32), Chr(48),Chr(49),Chr(50),Chr(51),Chr(52),Chr(53),Chr(54),Chr(55),Chr(56),Chr(57),Chr(65),Chr(66),Chr(67),Chr(68),Chr(69),Chr(70),Chr(71),Chr(72),Chr(73),Chr(74),Chr(75),Chr(76),Chr(77),Chr(78),Chr(79),Chr(80),Chr(81),Chr(82),Chr(83),Chr(84),Chr(85),Chr(86),Chr(87),Chr(88),Chr(89),Chr(90),Chr(97),Chr(98),Chr(99),Chr(100),Chr(101),Chr(102),Chr(103),Chr(104),Chr(105),Chr(106),Chr(107),Chr(108),Chr(109),Chr(110),Chr(111),Chr(112),Chr(113),Chr(114),Chr(115),Chr(116),Chr(117),Chr(118),Chr(119),Chr(120),Chr(121),Chr(122)]) 
  then 

(output := output + input[i];) ;

output

不完美,但它确实有效。有没有人有更好的解决方案?