Skip to main content
Known Participant
August 30, 2008
Question

Removing Whitespace from within a string

  • August 30, 2008
  • 4 replies
  • 4444 views
I have a string that I'm trying to remove any HTML existing within it.

Here is the REReplace:
<cfset VARIABLES.stripped_content = REReplaceNoCase(getevents.calevent_content,"<[^>]*>","","ALL") >

After this I'm trying to output the first 97 characters of the screen as preview text...however that reg replace leaves a ton of whitespace that appears to be messing up my Left() function.

How do I get rid of all that whitespace generated from the rereplace??

Thanks!!
Paul
    This topic has been closed for replies.

    4 replies

    Known Participant
    September 2, 2008
    Well..I thought I tried that previously but apparently I just copy and pasted the REReplace...which obviously wouldn't work. I didn't realize it until your comment. So I tried it and it does better, but there is still some undesired spaces. I'll just deal with it, it's just trying to compensate for some legacy DB entries.

    Thanks!
    Paul
    Known Participant
    September 2, 2008
    hmm.. maybe thats the problem then. I'm running CF 6.1 or something.
    Inspiring
    September 2, 2008
    paulferree wrote:
    > hmm.. maybe thats the problem then. I'm running CF 6.1 or something.

    Perhaps. I am not aware of any behavior differences with 6.1. The only whitespace issues I can think of are related to cffunctions and occasional strange behavior when output="true".

    In the worth a shot category, you might try a plain replace(...). Replace two spaces consecutive spaces with a single space.
    Known Participant
    August 30, 2008
    No, it's literally:

    <cfset VARIABLES.stripped_content = REReplaceNoCase(getevents.calevent_content,"<[^>]*>","","ALL") > <cfoutput>#Left(VARIABLES.stripped_content,97)#</cfoutput>

    I do need to say that the whitespace isn't viewable on the browser, but in the code view I see it...and when I run the Left() function it seems to be including the whitespace in the count...which is the problem.

    Inspiring
    August 30, 2008
    > I do need to say that the whitespace isn't viewable on the browser, but in the code view I see it.

    Yes, I understand. I tried your expression and it does not add any extra whitespace for me. If you run these lines, what results do you get?

    <cfsavecontent variable="getevents.calevent_content">
    This is plain text <br class="clear-both"> with some html <h4>thrown in</h4> from
    time to literal time ..<p class="time stamp-date">08/30/2008 12:18:17 PM</p>
    </cfsavecontent>

    <!--- trim the value _before_ the regex --->
    <cfset VARIABLES.stripped_content = REReplaceNoCase(Trim(getevents.calevent_content),"<[^>]*>","","ALL") >
    <cfoutput>
    length = #len(VARIABLES.stripped_content)#<br>
    <pre>|#VARIABLES.stripped_content#|</pre><br>
    left(97) = #left(VARIABLES.stripped_content, 97)#
    </cfoutput>
    Known Participant
    September 1, 2008
    Your example went fine..but when I replaced your HTML with a snippet of the actual HTML being evaluated I get the same problem...here it is.

    The Code:

    =======
    <cfsavecontent variable="getevents.calevent_content">
    <p class="MsoNormal" style="MARGIN: 0in 0in 0pt">
    <font color="#d3d3d3">
    <font face="Arial">
    <span style="FONT-SIZE: 10pt; FONT-FAMILY: Verdana">9am-1:30pm<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /??><o:p></o:p></span>
    <font size="2">
    </font>

    </font>
    </font>
    </p>
    <p class="MsoNormal" style="MARGIN: 0in 0in 0pt">
    <font color="#d3d3d3">
    <font face="Arial">
    <span style="FONT-SIZE: 10pt; FONT-FAMILY: Verdana">Contact:<span style="mso-spacerun: yes"> </span>Some Name @ 555-555-5555<o:p></o:p></span>

    <font size="2">
    </font>
    </font>
    </font>
    </p>
    </cfsavecontent>

    <!--- trim the value _before_ the regex --->
    <cfset VARIABLES.stripped_content = REReplaceNoCase(Trim(getevents.calevent_content),"<[^>]*>","","ALL") >
    <cfoutput>
    length = #len(VARIABLES.stripped_content)#<br>
    <pre>|#VARIABLES.stripped_content#|</pre><br>
    left(97) = #left(VARIABLES.stripped_content, 97)#
    </cfoutput>
    =======

    The Output (notice the whitespace):
    =======

    length = 180

    |


    9am-1:30pm









    Contact: Some Name @ 555-555-5555





    |


    left(97) = 9am-1:30pm
    =======

    Could have something to do with that UGLY HTML that I have to work with. However, in ASP this was outputted on the legacy site using:

    <%# this.GetChars(Eval("content") as String, 75) %>

    Seemed to work ok but I can't really test it.

    Thanks!
    Paul
    Inspiring
    August 30, 2008
    paulferree wrote:
    > How do I get rid of all that whitespace generated from the rereplace??

    I suspect it is not the rereplace. Are you using a cffunction by any chance?