473,325 Members | 2,712 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

Converting certain newlines to <br />s

I have a site wherein the clients can update the individual pages by editing
the text of that page in a <textarea>. Then, when the page is displayed,
all of the newlines (\n) are converted to HTML line breaks (<br />).
However, I have come across some instances where I do not want to convert
the newlines, such as the following:

<table>
<tr>
....
</tr>
</table>

Obviously, I would not want this to be

<table><br />
<tr><br />
....
</tr><br />
</table><br />

Can anyone suggest a method for achieving this? I've tried different
mixtures of regular expressions, but my skills with those are just not up to
par.

Christopher Finke
Jul 17 '05 #1
4 4533

"Christopher Finke" <ch**************@hotmail.com> wrote...

: Can anyone suggest a method for achieving this? I've tried different
: mixtures of regular expressions, but my skills with those are just not up
to
: par.

Christopher, first off it's never a good idea to allow people to enter html
code in forms of a content management service. You can allow them to insert
simple tags like <i>,<b> and <u> but if you're allowing them to create
things like tables it could seriously screw up the design of the site.

A practical solution for your problem would be to do a str_replace on the
text before you store it. For example:

$remove = array("<table>\n", "<table>\r", "<table>\r\n");
$text = str_replace($remove, '<table>', $text);

see: http://www.php.net/str_replace

Hope it helps,
..soma
Jul 17 '05 #2
somaBoy MX wrote:
"Christopher Finke" <ch**************@hotmail.com> wrote...

: Can anyone suggest a method for achieving this? I've tried different
: mixtures of regular expressions, but my skills with those are just not up
to
: par.

Christopher, first off it's never a good idea to allow people to enter html
code in forms of a content management service. You can allow them to insert
simple tags like <i>,<b> and <u> but if you're allowing them to create
things like tables it could seriously screw up the design of the site.

A practical solution for your problem would be to do a str_replace on the
text before you store it. For example:

$remove = array("<table>\n", "<table>\r", "<table>\r\n");
$text = str_replace($remove, '<table>', $text);

see: http://www.php.net/str_replace

Hope it helps,
.soma


Well, depending on how much work you want to do, which wouldn't be all
that much really, you could let them put tables in providing that you
check the syntax, running it through PHP's XML Parser actually may do
the trick, in the effort to make sure for every opening tag we have a
closing tag because tables are only really a pain in that scenario if
they close and don't open or such like.
~Cameron
Jul 17 '05 #3
On 2004-01-29, Christopher Finke <ch**************@hotmail.com> wrote:
I have a site wherein the clients can update the individual pages by editing
the text of that page in a <textarea>. Then, when the page is displayed,
all of the newlines (\n) are converted to HTML line breaks (<br />).
However, I have come across some instances where I do not want to convert
the newlines, such as the following:

<table>
<tr>
...
</tr>
</table>

Obviously, I would not want this to be

<table><br />
<tr><br />
...
</tr><br />
</table><br />


I think that if you let them write <table> etc tags, they should be
aware of the fact that newlines don't show up the way they expect it
too.

If you use output buffering, you could run tidy before outputting the
lot.
--
http://home.mysth.be/~timvw
Jul 17 '05 #4
It can't be done. You either accept HTML as input or accept plain text. Your
computer can't resolve the ambiguity when you mix the two up.

Uzytkownik "Christopher Finke" <ch**************@hotmail.com> napisal w
wiadomosci news:bv************@ID-158177.news.uni-berlin.de...
I have a site wherein the clients can update the individual pages by editing the text of that page in a <textarea>. Then, when the page is displayed,
all of the newlines (\n) are converted to HTML line breaks (<br />).
However, I have come across some instances where I do not want to convert
the newlines, such as the following:

<table>
<tr>
...
</tr>
</table>

Obviously, I would not want this to be

<table><br />
<tr><br />
...
</tr><br />
</table><br />

Can anyone suggest a method for achieving this? I've tried different
mixtures of regular expressions, but my skills with those are just not up to par.

Christopher Finke

Jul 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: fis | last post by:
Hi all, I've problem because there are needed break lines in my texts on the web site but i can't do it :( My pipeline looks like: XMS -> I18N -> XSLT -> HTML I have lot of texts in my...
6
by: Lasse | last post by:
I have done this simple function, it seems to work as intended, to solve a problem i have had for a while. I couldnt find any sample around that was working for me. I would like to test it with...
4
by: lkrubner | last post by:
PHP has a function called nl2br() which takes all the newlines in a string and turns them into break tags <br>. I need to do the same in javascript. Is this about right? I'm not sure how one is...
7
by: Rocky Moore | last post by:
I have a web site called HintsAndTips.com. On this site people post tips using a very simply webform with a multi line TextBox for inputing the tip text. This text is encode to HTML so that no...
2
by: Winshent | last post by:
I have a multi line text in an admin page on my cms. I am trying to capture carriage returns as and replace them with <p></p> bfore the string gets written to the database. I have tried all...
1
by: Winshent | last post by:
I have a multi line text in an admin page on my cms. I am trying to capture carriage returns as and replace them with <p></p> bfore the string gets written to the database. I have tried all...
3
by: ajay2552 | last post by:
Hi, I have a query. All html tags start with < and end with >. Suppose i want to display either '<' or '>' or say some text like '<Company>' in html how do i do it? One method is to use &lt,...
7
by: Nathan Sokalski | last post by:
Something that I recently noticed in IE6 (I don't know whether it is true for other browsers or versions of IE) is that it renders <br/and <br></br> differently. With the <br/version, which is what...
4
by: ismailc | last post by:
Hi, I need help please. In <TD> certain length/max length then force <BR> I would like within a <TD> to allow certain amount of char and then force a <br>. Is this possible, Please Assist!...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.