Replacing - and not Replacing...

Discussion in 'ASP General' started by Rob Meade, Apr 5, 2005.

  1. Rob Meade

    Rob Meade Guest

    Hi all,

    Been a long time since I've been here...

    /me waves to all..

    Ok - my conundrum..

    I have a form where a user can enter text and BB codes...for example:

    bold text

    I then parse the form when submitted with something like this:

    strContent = Replace(strContent, "", "<b>")
    strContent = Replace(strContent, "
    ", "</b>")

    Content is then written to the database, when displayed out comes the html.
    If they want to edit the content again it goes through the parser again but
    in reverse affectively and puts all the square brackets back in...

    My problem is around the carriage returns...

    At the moment I am replacing a vbCRLF with <br> - the problem is when they
    have something like this:

    [table]
    [tr]
    [td]my cell[/td][td]my second cell[/td]
    [/tr]
    [/table]

    I get a load of <br>'s that I don't really want, and of course because the
    table stuff in this case is replaced with the html for the table the <br>'s
    appear above it pushing it down the page - nightmare...

    What I am after I suppose is a way of being able to say - change a vbCRLF
    for a <br> but only if it's not proceeding any of the following....

    My initial thoughts were parse again - and remove and <br>'s if they are
    joined to say any of the above tags...

    strContent = Replace(strContent, "</tr><br>", "</tr>")

    for example...but it's not pretty, it doesn't feel "clever" and would
    require a load of additional parsing where mistakes could easily be made...

    The square bracketting tags etc will remain regardless of how this is
    resolved, so please no suggestions for css etc, but if anyone has any
    'compatible' :) ideas please let me know.

    Regards

    Rob
    Rob Meade, Apr 5, 2005
    #1
    1. Advertising

  2. Rob Meade

    Rob Meade Guest

    "Curt_C [MVP]" wrote ...

    >I dont know RegEx that well but maybe only do your replace if the <br> is
    >after a ] and before a [/


    Hi Curt, thanks for the reply.

    I am not overly familar with RegEx myself, but will take a look...I'm not
    sure where I'll need to do the replaces anyway, as they could have this:

    Hello Curt,

    Welcome to our website.

    Enjoy your visit.


    So, that would look something like this:

    Hello <b>Curt</b>,<br><br>Welcome to our website.<br><br>Enjoy <i>your</i>
    visit.

    In this case I'd want the vbCRLF's changed to <br>'s are the end of each
    line, and on the blank lines, and obviously that wouldn't be between a ] and
    a [/ etc...

    Another difficulty are the tags that they can still enter html attributes
    in...for example:

    I enable them to have

    [table] or [table ...]

    with ... being whatever they want to enter, perhaps [table width="100"] when
    I parse this its more difficult because you dont have a predetermined piece
    of text, they could enter 300, 300%, 30000 for example...

    Regards

    Rob
    Rob Meade, Apr 5, 2005
    #2
    1. Advertising

  3. "Rob Meade" <> wrote in message
    news:53B4e.4572$...
    > Hi all,
    >
    > Been a long time since I've been here...
    >
    > /me waves to all..
    >
    > Ok - my conundrum..
    >
    > I have a form where a user can enter text and BB codes...for example:
    >
    > bold text
    >
    > I then parse the form when submitted with something like this:
    >
    > strContent = Replace(strContent, "", "<b>")
    > strContent = Replace(strContent, "
    ", "</b>")
    >
    > Content is then written to the database, when displayed out comes the
    > html. If they want to edit the content again it goes through the parser
    > again but in reverse affectively and puts all the square brackets back
    > in...
    >
    > My problem is around the carriage returns...
    >
    > At the moment I am replacing a vbCRLF with <br> - the problem is when they
    > have something like this:
    >
    > [table]
    > [tr]
    > [td]my cell[/td][td]my second cell[/td]
    > [/tr]
    > [/table]
    >
    > I get a load of <br>'s that I don't really want, and of course because the
    > table stuff in this case is replaced with the html for the table the
    > <br>'s appear above it pushing it down the page - nightmare...
    >
    > What I am after I suppose is a way of being able to say - change a vbCRLF
    > for a <br> but only if it's not proceeding any of the following....
    >
    > My initial thoughts were parse again - and remove and <br>'s if they are
    > joined to say any of the above tags...
    >
    > strContent = Replace(strContent, "</tr><br>", "</tr>")
    >
    > for example...but it's not pretty, it doesn't feel "clever" and would
    > require a load of additional parsing where mistakes could easily be
    > made...
    >
    > The square bracketting tags etc will remain regardless of how this is
    > resolved, so please no suggestions for css etc, but if anyone has any
    > 'compatible' :) ideas please let me know.
    >
    > Regards
    >
    > Rob


    Why don't you simply store the content as submitted, then parse to html when
    you need to display? This is similar to data entered in textarea controls.
    You don't insert <br> tags when you capture the data to a database, only
    when you display it.
    Chris Hohmann, Apr 6, 2005
    #3
  4. Rob Meade

    Rob Meade Guest

    "Curt_C [MVP]" wrote ...

    > Then you are probably going to have to train your users. I mean if they
    > put in a break then THEY put in a break. You are just doing what they
    > want.


    Users eh! *tut* :eek:)

    If I did that though, the tables support for example to appear correctly
    would have to be entered all on one line...a complex table layout would be
    almost impossible to decipher if there was anything wrong with it, if they
    enter it all on one line that's up to them, but my parsing I believe needs
    to ignore the carriage returns in between table tags (opening and closing) -
    when after cells and rows anyway...

    Regards

    Rob
    Rob Meade, Apr 11, 2005
    #4
  5. Rob Meade

    Rob Meade Guest

    "Chris Hohmann" wrote ...

    > Why don't you simply store the content as submitted, then parse to html
    > when you need to display?


    Hi Chris, thanks for the reply.

    I thought of this when I first starting writing it some 2+ years ago, the
    site in question gets a reasonable amount of traffic each month, the biggest
    I've worked on thus far, not necessarily huge in comparision to Amazon and
    the like though, but still, I decided to parse once, store to SQL Server,
    then on the displaying page all the app has to do is get the content and
    dump to the page, no further formatting is needed because the CSS handles
    the rest - if I did it the other way, *every* user would trigger a parse of
    the content for every page - I figured this would cause a bit of a
    performance over head.

    > This is similar to data entered in textarea controls. You don't insert
    > <br> tags when you capture the data to a database, only when you display
    > it.


    Normally I'd agree with that, if it was just some text and some carriage
    returns, but because they are entering all kinds of formatting in there as
    well, bold, italics, lists, tables and the like that needs to be changed
    anyway, so it all gets done in the one swoop..

    Regards

    Rob
    Rob Meade, Apr 11, 2005
    #5
  6. "Rob Meade" <> wrote in message
    news:wXx6e.9695$...
    > "Chris Hohmann" wrote ...
    >
    >> Why don't you simply store the content as submitted, then parse to html
    >> when you need to display?

    >
    > Hi Chris, thanks for the reply.
    >
    > I thought of this when I first starting writing it some 2+ years ago, the
    > site in question gets a reasonable amount of traffic each month, the
    > biggest I've worked on thus far, not necessarily huge in comparison to
    > Amazon and the like though, but still, I decided to parse once, store to
    > SQL Server, then on the displaying page all the app has to do is get the
    > content and dump to the page, no further formatting is needed because the
    > CSS handles the rest - if I did it the other way, *every* user would
    > trigger a parse of the content for every page - I figured this would cause
    > a bit of a performance over head.
    >
    >> This is similar to data entered in textarea controls. You don't insert
    >> <br> tags when you capture the data to a database, only when you display
    >> it.

    >
    > Normally I'd agree with that, if it was just some text and some carriage
    > returns, but because they are entering all kinds of formatting in there as
    > well, bold, italics, lists, tables and the like that needs to be changed
    > anyway, so it all gets done in the one swoop..
    >
    > Regards
    >
    > Rob
    >


    Ok. It looks like you have a few options here:
    1. Create a complex de-parsing algorithm that distinguished between display
    linebreaks and markup linebreaks. This methodology will never be foolproof.

    2. Introduce an explicit break tag into your custom markup language, perhaps
    ""? This of course raises the question, "Why use a custom markup at all?
    Why not simply use HTML to begin with?"

    3. Save both the original text and the parsed text when inserting/updating
    data. To improve performance, you could save the original and parsed text to
    files and simply store file references in the database. Here's an article on
    the merits of storing file references instead of the actual data. It focuses
    on image files, but it actually applies to any large file:
    http://aspfaq.com/show.asp?id=2149

    HTH
    -Chris Hohmann
    Chris Hohmann, Apr 11, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    393
  2. Nico Grubert
    Replies:
    9
    Views:
    305
    John Machin
    Nov 28, 2004
  3. Alexandre Cecchi
    Replies:
    5
    Views:
    331
    Jerry Sievers
    Nov 27, 2004
  4. Adam Monsen
    Replies:
    3
    Views:
    1,052
    Arne Vajhøj
    Jan 27, 2009
  5. goodman
    Replies:
    4
    Views:
    636
    Lawrence D'Oliveiro
    Nov 5, 2010
Loading...

Share This Page