Skip to main content
September 11, 2009
Answered

coldfusion for remove a string and save as new string

  • September 11, 2009
  • 13 replies
  • 2735 views

I have a big string that I want to remove a part of it and save it as a new string?  I've tried replace, find ect...and I dont think there is a function that allow me to do that.  any ideas?

thanks

    This topic is closed to new replies. Start a new post to keep the conversation going.
    Correct answer JR__Bob__Dobbs-qSBHQ2

    can you clarify a little bit more?  thanks.


    Add any lines not matching the To and From lines to a new variable.

    <cfset newText="" />

    <cfloop list="#email_list.body#" delimiters="#chr(13)##chr(10)#" index="line">

                      //* replace the whole from line with empty *//

                 <cfif REFindNoCase("From:.*",line)>

                     <cfset from_line = line />

                    <cfset from_line = #REReplaceNoCase("#from_line#", "From:.*", "")# />

                   //* replace the whole To line with empty *//

                 <celsefif REFindNoCase("To:.*",line)>

                     <cfset to_line = line />

                    <cfset to_line = #REReplaceNoCase("#to_line#", "To:.*", "")# />

                <cfelse>

                    <!--- if not removing line add to newText string --->

                    <cfset newText=newText & line & chr(13) & chr(10) />

                </cfif>

    </cfloop>

    <cfoutput>#newText#</cfoutput> <!--- email text without to and from lines --->

    This was truncated when I posted it previously.

    13 replies

    Prasanth_Kumar_S
    Inspiring
    September 11, 2009

    another method to correctly get the content, but a bit tricky. See code below.

    <cfset emailvar = "From: jane doe  Sent: Thursday, September 10, 2009 1:01 PM">
    <cfset fromPos = findnocase("From:",emailvar)>
    <cfset toPos = findnocase("Sent:",emailvar) - (fromPos + 5)>
    <cfoutput>
         between from and to : #mid(emailvar,fromPos+5,toPos)#<br />
    </cfoutput>

    using the findnocase you can get the position and use mid get the characters between the two points.

    -Prasanth

    Inspiring
    September 11, 2009

    You could also use regular expression match lines starting with "From" and containing text that looks like an email address.

    <cfsavecontent variable="emailText">
    From: jane doe [mailto:jdoe@mail.com]
    Sent: Thursday, September 10, 2009 1:01 PM
    To: jdoe@mail.com
    Subject: this is the subject line

    Dear Jon Doe,

    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed eget varius enim. Ut nec erat quis est dignissim tincidunt. Sed ac lacus dolor. Curabitur id fringilla metus. Phasellus in justo non lacus malesuada semper quis eu libero. Donec in libero ut augue ultricies lobortis. Mauris porttitor tristique cursus. Vivamus neque leo, iaculis tincidunt tristique a, lacinia vitae sem. Nunc eget vulputate ligula. Phasellus ut arcu risus, et luctus elit. Vestibulum eu ante neque. Curabitur at massa nec lectus vehicula pretium. Nunc id eros est, sit amet pellentesque tortor. Integer et ante magna. Donec pellentesque odio vitae augue adipiscing non vestibulum nibh porttitor.

    Vestibulum consectetur ligula eu elit egestas eu luctus erat bibendum. Vivamus pellentesque eleifend luctus. Curabitur id nibh id lacus adipiscing iaculis vel at neque. Ut sit amet enim sem, dignissim posuere urna. Maecenas ac elit vitae turpis sagittis tempus. Aliquam aliquam velit vitae urna tempor hendrerit. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Ut non ligula nec tellus egestas ultricies sed eu ante. Aliquam quam mauris, bibendum id varius id, iaculis at lectus. Fusce nunc sapien, cursus sit amet consectetur vel, eleifend ac diam. Aliquam dictum tempor accumsan.
    </cfsavecontent>


    <cfset fromMatches=ReMatch(".{0,1}From\: [ \w\[\]\:@\.,;]*", emailText) />


    <cfif ArrayLen(fromMatches) gt 0>

        <cfoutput>
            <cfloop from="1" to="#ArrayLen(fromMatches)#" index="idx">
                #Trim(Right(fromMatches[idx], Len(fromMatches[idx]) - 6))#  <!--- omit "From: " to get the name and email address --->
            </cfloop>
        </cfoutput>

    <cfelse>
        NO MATCHES FOUND
    </cfif>

    Inspiring
    September 11, 2009

    Can you provide an explanation, with samples, of what you are trying to do?  Please provide the code you've already tried. Explain what did not work: what was the expected result versus what was the actual result.  Please include any error messages you received.

    September 11, 2009

    Thanks.  I do not know if there is a tag or function that will work.  Basically, I have text email that I use cfpop to pull out the email body


    The text content as follow,  So I want to get the From and the Sent out the this text and save as its own string.  After I save those, I can delete them.

    Thanks.

    From: jane doe [mailto:jdoe@mail.com] 
    Sent: Thursday, September 10, 2009 1:01 PM
    To: jdoe@mail.com
    Subject: this is the subject line




    Dear Jon Doe,



    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed eget varius enim. Ut nec erat quis est dignissim tincidunt. Sed ac lacus dolor. Curabitur id fringilla metus. Phasellus in justo non lacus malesuada semper quis eu libero. Donec in libero ut augue ultricies lobortis. Mauris porttitor tristique cursus. Vivamus neque leo, iaculis tincidunt tristique a, lacinia vitae sem. Nunc eget vulputate ligula. Phasellus ut arcu risus, et luctus elit. Vestibulum eu ante neque. Curabitur at massa nec lectus vehicula pretium. Nunc id eros est, sit amet pellentesque tortor. Integer et ante magna. Donec pellentesque odio vitae augue adipiscing non vestibulum nibh porttitor.



    Vestibulum consectetur ligula eu elit egestas eu luctus erat bibendum. Vivamus pellentesque eleifend luctus. Curabitur id nibh id lacus adipiscing iaculis vel at neque. Ut sit amet enim sem, dignissim posuere urna. Maecenas ac elit vitae turpis sagittis tempus. Aliquam aliquam velit vitae urna tempor hendrerit. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Ut non ligula nec tellus egestas ultricies sed eu ante. Aliquam quam mauris, bibendum id varius id, iaculis at lectus. Fusce nunc sapien, cursus sit amet consectetur vel, eleifend ac diam. Aliquam dictum tempor accumsan.



    Inspiring
    September 11, 2009

    Try treating the entire string as a list delimited by chr(13).  Output the first couple of elements.  If they are the To and Sent lines, treat those lines as lists delimited by a colon.  You want listrest().