SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot
    Join Date
    Oct 2001
    Location
    Montreal, Canada
    Posts
    169
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Replace line feeds with ASP

    Hi people,

    I know that with the PHP ereg_replace() function you can replace line feeds and carriage return in text that comes from a form textarea element to format HTML like this:

    PHP Code:
    $variable ereg_replace("\r","",$variable);
    $variable ereg_replace("\n\n","<P>",$variable);
    $variable ereg_replace("\n","<BR>",$variable); 
    I tried to do the same with ASP using the Replace VBScript function, but that don't work. I guess I'm doing it the wrong way. Or em using the wrong function. Here is my code:

    Code:
    strVariable = Replace(strVariable, "\r", "", 1, -1, 1)
    strVariable = Replace(strVariable, "\n\n", "<p>", 1, -1, 1)
    strVariable = Replace(strVariable, "\n", "<br>", 1, -1, 1)
    What is wrong with this code?

    Thanks

    Mike

  2. #2
    SitePoint Addict
    Join Date
    Jun 2001
    Location
    Newcastle, England
    Posts
    268
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Instead of "\n" etc., there are special constants in ASP.

    Try

    strVariable = Replace(strVariable, vbCrLf, "<br>")
    strVariable = Replace(strVariable, vbCrLf & vbCrLf, "<p>")

    etc.

    vbCrLf is a carriage return/linefeed pair (standard enter character on Windows, ASCII 13)
    vbCr is a carriage return
    vbLf is a linefeed


    Hope that helps .
    Nick Wilson [ - email - ]

  3. #3
    SitePoint Zealot
    Join Date
    Oct 2001
    Location
    Montreal, Canada
    Posts
    169
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, that work.

  4. #4
    SitePoint Addict
    Join Date
    Jun 2001
    Location
    Newcastle, England
    Posts
    268
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Good good .

    Of course you spotted the "deliberate" mistake (with the 2 lines in that order, the second will never be used as all vbCrLf's will already have been replaced with <br>'s - you'd need them the other way round )

    I knew that when I psoted, hosest...

    *wanders off whistling and looking innocent*
    Nick Wilson [ - email - ]

  5. #5
    SitePoint Zealot
    Join Date
    Oct 2001
    Location
    Montreal, Canada
    Posts
    169
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, I don't really know (not yet), but if I look at the source of my newly created html file, I have <p> and <br> tags where I wanted to have them.

  6. #6
    ALT.NET - because we need it silver trophybronze trophy dhtmlgod's Avatar
    Join Date
    Jul 2001
    Location
    Scotland
    Posts
    4,836
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by CrazyCrane
    Good good .

    Of course you spotted the "deliberate" mistake (with the 2 lines in that order, the second will never be used as all vbCrLf's will already have been replaced with <br>'s - you'd need them the other way round )

    I knew that when I psoted, hosest...

    *wanders off whistling and looking innocent*
    Course you did mate...



    If you wanted to distinguise(sp?) between a </p> and <br> you could try:
    Code:
    strVariable = Replace(strVariable, chr(10), "</p>") 
    strVariable = Replace(strVariable, chr(13), "<br>")


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
  •