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:

    [td]my cell[/td][td]my second cell[/td]

    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.


    Rob Meade, Apr 5, 2005
    1. Advertisements

  2. Rob Meade

    Rob Meade Guest

    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>

    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...


    Rob Meade, Apr 5, 2005
    1. Advertisements

  3. 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
  4. Rob Meade

    Rob Meade Guest

    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...


    Rob Meade, Apr 11, 2005
  5. Rob Meade

    Rob Meade Guest

    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.
    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..


    Rob Meade, Apr 11, 2005
  6. 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:

    -Chris Hohmann
    Chris Hohmann, Apr 11, 2005
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.