Replacing - and not Replacing...

R

Rob Meade

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:

my cellmy second cell

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
 
R

Rob Meade

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

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
 
C

Chris Hohmann

Rob Meade said:
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:

my cellmy second cell

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

Rob Meade

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

Rob Meade

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

Chris Hohmann

Rob Meade said:
...


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.


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
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top