Search code examples
htmlcoldfusionwhitespacecoldfusion-8

How to remove more than one whitespace character from HTML?


I want to remove extra whitespace which is coming from the user end, but I can't predict the format of the HTML.

For example:

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

Please guide me on how to remove more than one whitespace character from HTML.


Solution

  • You could use regex to replace any cases of multiple whitespace characters with a single space by looping over the result until no more multiple whitespace occurances exist:

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

    Tested working in CF10.