jackncoke
jackncoke

Reputation: 2020

VBScript text Formatting issues

I have data that has <br> and <BR> Tags in it. I have to update a old site that is running VBScript Classic ASP. I know little to nothing about either of these two but i came up with some hackish logic to try and quickly resolve the problem and i do not understand why it is not working.

    If InStr(1,objRecN("News"),"<BR>")> 1 Then
     response.write "This is 1<BR>"
     body = Replace(objRecN("News"),"<BR>", vbCrLf)
Else
     response.write "This is 2<br>"
     body = Replace(objRecN("News"),"<br>", vbCrLf)
End If

The Response.Writes are working effectively in proving that the right logic is being applied but the replace is not working.

 body = Replace(Replace(objRecN("News"),"<br>", vbCrLf), "<BR>", vbCrLf)

Also this is not working. This was my first attempt at trying to resolve this. Any clues?

As soon as other logic is applied it breaks!

  If InStr(1,objRecN("News"),"<BR>")> 1 Then
    response.write "This is 1<BR>"
     body = Replace(objRecN("News"),"<BR>", vbCrLf )
    End If

Upvotes: 1

Views: 266

Answers (2)

jackncoke
jackncoke

Reputation: 2020

Thank you everyone for your help.

body = Replace(body,"<BR>", "<br>")

Although i learned a lot from your posts. This is the code that actually fixed my problem. I am sorry if i didn't not convey it better.

Upvotes: 0

Ekkehard.Horner
Ekkehard.Horner

Reputation: 38755

All hard info (c) @Alex.

VBScript's Replace function works; it can even be asked to ignore case (mark the use of pre-defined vbTextCompare instead of magic number). If it does not seem so, the programmer is to blame. Evidence:

>> s = "abc<br>def<BR>ghi"
>> WScript.Echo qq(s)
>> s = Replace(s, "<br>", "!!!!", 1, -1, vbTextCompare)
>> WScript.Echo qq(s)
>>
"abc<br>def<BR>ghi"
"abc!!!!def!!!!ghi"

If you replace with vbCrLf (or other whitespace) and then write the result to HTML, you won't 'see' the hard work Replace did for you.

If you insist on two (or more) replacements, you have to feed the previous result to the current operation. That's why your

body = Replace(objRecN("News"),"<BR>", vbCrLf)
body = Replace(objRecN("News"),"<br>", vbCrLf)

'work' separately but not together. In contrast, both

body = objRecN("News")
body = Replace(body,"<BR>", vbCrLf)
body = Replace(body,"<br>", vbCrLf)

or

body = Replace(Replace(objRecN("News"),"<br>", vbCrLf), "<BR>", vbCrLf)

will deal with all <BR> and <br>, but not with <Br>, which is no problem for the vbTextCompare version.

If you next data contain <br/>, <br />, and <br />, you'll need a regular expression.

Upvotes: 2

Related Questions