2

我想删除来自用户端的额外空格,但我无法预测 HTML 的格式。

例如:

<p> It's interesting that you would try cfsetting, since nothing in it's
documentation would indicate that it would do what you are asking.
Unless of course you were mis-reading what "enableCFoutputOnly" is
supposed to do.


</p>



<p>



It's interesting that you would try cfsetting, since nothing in it's
documentation would indicate that it would do what you are asking.
Unless of course you were mis-reading what "enableCFoutputOnly" is
supposed to do.</p>

请指导我如何从 HTML 中删除多个空白字符。

4

3 回答 3

1

您可以使用正则表达式通过循环结果来用单个空格替换多个空格字符的任何情况,直到不再存在多个空格:

lastTry = "<p>   lots of space    </p>";
nextTry = rereplace(lastTry,"\s\s", " ", "all");
while(nextTry != lastTry) {
  lastTry = nextTry;
  nextTry = REReplace(lastTry,"\s\s", " ", "all");
}

经测试在 CF10 中工作。

于 2012-08-18T06:37:03.543 回答
0

如果您出于完全的懒惰而不想通过代码来做到这一点

=> http://jsbeautifier.org/

如果你想通过代码来做,那么正则表达式将是另一种选择

于 2012-08-18T06:06:42.543 回答
0

这应该这样做:

<cfscript>
  string function stripCRLFAndMultipleSpaces(required string theString) {
    local.result = trim(rereplace(trim(arguments.theString), "([#Chr(09)#-#Chr(30)#])", " ", "all"));
    local.result = trim(rereplace(local.result, "\s{2,}", " ", "all"));
    return local.result;
  }
</cfscript>
于 2014-02-04T10:28:44.960 回答