SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    Posts rarely lloydi's Avatar
    Join Date
    Jan 2002
    Location
    Swindon UK
    Posts
    620
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Trouble reading from TextArea in ASP

    OK, here's the deal: I am trying to put together a little ASP page that lets you paste a block of HTML in a text area then, at the click of a button, convert it to Javascript document.write statements (including any concatenation, switching of single and double quotes). A really useful tool!

    However, I cannot get the darn thing to work. What I am doing is:

    * reading the whole string
    * looping through all characters, building up a result string for each line
    * where a carriage return is found, start a new line
    * finally join up all lines to create a final output.

    I have attached an image, which shows the problem. For some reason, after the first line, a vbcr is appearing which I cannot account for - I do not know where it is coming from. I have spent a good hour looking at just five or so lines of code and it's driving me nuts.

    Please can you offer any help?

    The image is attached, and here is the code:

    ===================================================

    <%
    dim intStringLength, charThisChar, intLineCount
    dim arrLineNo(999)
    dim strTemp
    intLineCount = 0
    strOutput = ""
    strInput = strInput & vbcr
    intStringLength = len(strInput)

    'loop through all characters in the string, including carriage returns
    for i=1 to intStringLength
    charThisChar = mid(strInput,i,1) '-current character
    if NOT (charThisChar = vbcr) then '-if it's any character OTHER than a carriage return
    strTemp = strTemp & charThisChar 'pass into a temporary string for this line in the text input
    else
    strOutput = strOutput & "LINE_START>" & strTemp & "(" & len(strTemp) & ")" & "<LINE_END" & vbcr 'add to the overall output
    strTemp="" 'reset temp string
    end if
    next
    %>
    <textarea name="txtOutput" rows="10" cols="40" wrap="off">
    <% = strOutput %>
    </textarea>
    Attached Images Attached Images
    Build Your Own Web Site the Right Way!
    A beginners' HTML/CSS book with web standards at its heart
    The Ultimate HTML Reference
    A complete reference, in glorious hardback

  2. #2
    SitePoint Enthusiast PeteWJ's Avatar
    Join Date
    Oct 2001
    Location
    UK
    Posts
    75
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The problem looks to be that you are not removing the linefeed character from the end of the line, you are just checking for the carriage return.

    A quick and dirty fix to your problem (although it would not work in every situation - but it would work under normal circumstances) is as follows:
    [VBS]<%
    for i=1 to intStringLength
    charThisChar = mid(strInput,i,1) '-current character
    If (charThisChar = vbLf) Then
    ' Do Nothing
    ElseIf NOT (charThisChar = vbcr) then '-if it's any character OTHER than a carriage return
    strTemp = strTemp & charThisChar 'pass into a temporary string for this line in the text input
    Else
    strOutput = strOutput & "LINE_START>" & strTemp & "(" & len(strTemp) & ")" & "<LINE_END" & vbcr 'add to the overall output
    strTemp="" 'reset temp string
    end if
    next
    %>
    <textarea name="txtOutput" rows="10" cols="40" wrap="off">
    <% = strOutput %>
    </textarea>[/VBS]It's not going to win any awards for clean code but it should work. Other approaches are available (such as use a Replace function to remove all of the line feed characters prior to running the loop, or use a split command and a For Each loop on the resulting array) but this is possibly the easiest to understand.

    Pete
    Last edited by PeteWJ; Mar 21, 2002 at 07:38.

  3. #3
    Posts rarely lloydi's Avatar
    Join Date
    Jan 2002
    Location
    Swindon UK
    Posts
    620
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks pete - you know, I wondered if there was something like this causing the problem, and I did have checks in there in an earlier version that looked for vbcr and also vbcrlf ... but I had no idea that there was such a thing as a vblf

    So, there was something invisible sneaking in that I didn't know about.
    As for clean code, well, it doesn't matter a huge amount - it's only a very tiny little tool, and it'll do the job!

    Thanks for this, I can now move on and finish the task at hand.

    Cheers
    Build Your Own Web Site the Right Way!
    A beginners' HTML/CSS book with web standards at its heart
    The Ultimate HTML Reference
    A complete reference, in glorious hardback


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •