Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从HTML中删除多个空白字符?_Html_Coldfusion_Whitespace_Coldfusion 8 - Fatal编程技术网

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

如何从HTML中删除多个空白字符?,html,coldfusion,whitespace,coldfusion-8,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 supp

我想删除来自用户端的额外空白,但我无法预测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>
有趣的是,您可以尝试cfsetting,因为其中没有任何内容是
文档将表明它可以满足您的要求。
当然,除非您误读了什么是“enableCFoutputOnly”
应该这样做。

有趣的是,您可以尝试使用cfsetting,因为其中没有任何内容是 文档将表明它可以满足您的要求。 当然,除非您误读了什么是“enableCFoutputOnly” 应该这样做


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

如果你不想完全懒散地通过代码来完成这项工作

=>


如果要通过代码执行,则正则表达式是另一个选项

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

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

”; nextTry=重新替换(lastTry、“\s\s”、“全部”); while(nextTry!=上次尝试){ lastTry=nextTry; nextTry=重新替换(lastTry、“\s\s”、“全部”); }
在CF10中测试工作。

这应该可以做到:

<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>

字符串函数stripCRLFAndMultipleSpaces(字符串的必需字符串){
local.result=trim(重新替换trim(arguments.theString),“([Chr(09)#-#Chr(30)#]),”,“all”);
local.result=trim(重新替换(local.result,“\s{2,}”,“全部”);
返回本地结果;
}

这是一种非常冗长的方法!最快的方法可能是:
input.replaceAll('\s\s+','')
或者,为了避免用空格替换换行符,也可以这样做:
input.replaceAll('(\s)\s+','$1')
(即将所有空格压缩到每个链中找到的第一个空格)。当然,所有这些都会修改pre/etc中的内容——如果这类内容很重要,那么一个合适的HTML解析器是更好的方法,尤其是因为已经有了一个“整洁”的方法来准确处理这种情况。旁注:由于OP明确地将问题标记为CF8,使用CF10进行测试有点多余(但是,这里的一切都足够简单,从CF7开始应该可以正常工作。)啊,我认为input.replaceAll(“(\s)\s+”,“$1”)是一个更好的解决方案。为什么不把它作为一个单独的答案呢?re:旁注:“这里的一切都足够简单,从CF7开始应该可以很好地工作”——没错。我对Russ答案的所有评论也适用于这里——这将破坏
pre
标记的格式,因为它将换行符转换为空格,也有可能破坏JavaScript功能。