我正在构建一个水晶报告,为第三方应用程序输出XML格式的文本。
第三方应用无法处理非字母数字字符,因此我必须将它们转换为HTML才能处理。目前,我使用此代码来捕获最常见的字符:
stringvar output := {table.fieldName};
output := Trim(output);
output := Replace (output,chrW(38),"&");
output := Replace (output,chrW(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,"<","<");
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,"`","`");
output := Replace (output,"{","{");
output := Replace (output,"|","|");
output := Replace (output,"}","}");
output := Replace (output,"~","~");
output := Replace (output, chrW(145),"‘");
output := Replace (output, chrW(146),"’");
output := Replace (output, chrW(147),"“");
output := Replace (output, chrW(148),"”");
output := Replace (output, chrW(8212),"—");
output := Replace (output, chrW(8217),"’");
output := Replace (output, chrW(8220),"“");
output := Replace (output, chrW(8221),"”");
output := Replace (output,"£","œ");
它不健康,需要维护。我在找到它们时添加了字符,但是我想知道是否有可能使用AscW和ChrW来动态识别和转换非字母数字字符的方法。字符串中的html / ascii等价物。
答案 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
不完美,但它确实有效。有没有人有更好的解决方案?