• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Using RIGHT, LEFT, and Replace, and getting really strange results! Help!

New Here ,
Aug 09, 2010 Aug 09, 2010

Copy link to clipboard

Copied

Hi all -

I need some help with this one.  I'm writing a script that pretty much parses out the header and footer of a saved HTML document, so I only have the middle of it (its a quote file), and then resaves it as a new file.  The header parsing (left) works perfectly, but when I get to the footer (right), it rips out everything EXCEPT for the header for some reason!  So I end up getting a file that is more or less the header and the footer, but not the meat of the quote file.

Here's my code:

<!---read file --->
<cffile action="read" file="#filestoragelocation#/#cffile.serverFile#" variable="quotecontent">
<!---remove header --->
<cffile action="read" file="#templatelocation#/remove1.html" variable="bannerold">
<!---add new header --->
<cffile action="read" file="#templatelocation#/add1-1.html" variable="bannernew">
<!---replace data --->
<cfset quoteedit=Replace(quotecontent, LEFT(quotecontent,find(bannerold,quotecontent)), bannernew)>
<!--bannerdone--->

<!---update footer --->
<cffile action="read" file="#templatelocation#/remove2.html" variable="oldfooter">
<cffile action="read" file="#templatelocation#/add2w.html" variable="newfooter">

<cfset quoteedit=Replace(quoteedit, RIGHT(quoteedit,find(oldfooter,quoteedit)), newfooter)>
<!---<cfset quoteedit=Replace(quoteedit, oldfooter, newfooter)>--->
<!---write file --->
<cffile action="write" file="#filestoragelocation#/M#cffile.serverFile#" output="#quoteedit#">

So pretty much what I've done with BannerOld and OldFooter is found a tag in the code that is only in the document once (with the banner, its an image, with the footer, its text ("The terms and conditions of the CO")...and hypothetically, with left, I should be selecting everything up to the image in the banner and replacing it, and with right, I should be selecting everything after the text and replacing it.

For whatever reason, instead, I'm just getting the new banner and the footer in the final document.  If I remove the footer code, it works perfectly (with of course, the old footer still being there).

I'm probably just having some dumb oversight here...but some help would be amazing.  Thanks as always guys!!

JE

TOPICS
Advanced techniques

Views

740

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Explorer , Aug 17, 2010 Aug 17, 2010

You say the first part is working so I will take your word for that, although it doesn't look right.

The second part need probably isn't working because youre calculating the Right X chars but you are counting the chars by finding the index of your find from the beginning.  So imagine you are replacing the first 10 chars with your new header.  Your doc is 100 chars total.  You wnat to replace the last 10 characters. Your find returns 90 because you started from the beginning.  You grab the Right

...

Votes

Translate

Translate
Enthusiast ,
Aug 10, 2010 Aug 10, 2010

Copy link to clipboard

Copied

You might try simplifying your code.  Just get the quote portion of the file then add your header and footer around it.

Something like:

<!--- original file --->


<cffile action="read" file="#filestoragelocation#/#cffile.serverFile#" variable="originalFile">

<!--- old header --->
<cffile action="read" file="#templatelocation#/remove1.html" variable="oldHeader">

<!--- old footer --->
<cffile action="read" file="#templatelocation#/remove2.html" variable="oldFooter">

<!--- new header --->
<cffile action="read" file="#templatelocation#/add1-1.html" variable="newHeader">

<!--- new footer --->
<cffile action="read" file="#templatelocation#/add2w.html" variable="newFooter">

<!--- remove header --->
<cfset quotePart=Replace(originalFile, oldHeader, "")>

<!--- remove footer --->
<cfset quotePart=Replace(quotePart, oldFooter, "")>

<!--- add new header and footer --->
<cfset newQuote=newHeader & quotePart & newFooter>

<!--- new file --->
<cffile action="write" file="#filestoragelocation#/M#cffile.serverFile#" output="#newQuote#">


It might be useful to have a basic example of the files you are working with.


Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Aug 13, 2010 Aug 13, 2010

Copy link to clipboard

Copied

Hey there Bob -

Thanks for the response...unfortunately...the Old Header and Old Footers can change dynamically because they're from a 3rd party website...so what i've tried to do is grab pieces of the formatting...because that rarely changes...that terminates the header and starts the footer...and then use left and right to capture the rest of the header and footer.  Does that make sense?  And the rest of the header and footer has information that may or may not stay consistent.  So unfortunately, its not as black and white as just selecting all of the header and footer and replacing them.

Thanks for giving it a shot though!  My code is really close...its just something about the logic is slightly off with the footer.

Thanks!
JE

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Aug 17, 2010 Aug 17, 2010

Copy link to clipboard

Copied

anyone?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Aug 17, 2010 Aug 17, 2010

Copy link to clipboard

Copied

Perhaps you could post some basic examples of the files you are working with so that we can re-create the problem you are having?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Aug 17, 2010 Aug 17, 2010

Copy link to clipboard

Copied

You say the first part is working so I will take your word for that, although it doesn't look right.

The second part need probably isn't working because youre calculating the Right X chars but you are counting the chars by finding the index of your find from the beginning.  So imagine you are replacing the first 10 chars with your new header.  Your doc is 100 chars total.  You wnat to replace the last 10 characters. Your find returns 90 because you started from the beginning.  You grab the Right 90 chars, taking you to the end of the header, and you are replacing all that with the footer.

What you want is Replace(quoteedit, RIGHT(quoteedit,Len(quoteedit)-find(oldfooter,quoteedit)), newfooter).  That should work.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Aug 18, 2010 Aug 18, 2010

Copy link to clipboard

Copied

LATEST

Ahhh you're right!  Didn't stop to think about where the count was counting from.  Thanks so much for the help!


JE

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation