如何从HTML中删除多个空格字符?

时间:2012-08-18 06:04:22

标签: html coldfusion whitespace coldfusion-8

我想删除来自用户端的额外空格,但我无法预测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中删除多个空白字符。

3 个答案:

答案 0 :(得分:0)

如果你不想通过完全懒惰的代码来做这件事

=&gt; {{3P>

如果您想通过代码执行,那么正则表达式将是另一种选择

答案 1 :(得分:0)

您可以使用正则表达式将多个空格字符的任何情况替换为单个空格,方法是循环遍历结果,直到不再存在多个空格:

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

在CF10中进行了测试。

答案 2 :(得分: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>