473,480 Members | 1,857 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

exeptions for nl2br --> preg_replace??

Hi,

Hope you can help me out with this one..

nl2br($string) converts all the \n to <br \>

... but ..

Suppose I have a table or other HTML elements in $string..
All the TD> and TR> and FORM> are trailed by <br />

How can I adjust the function:
preg_replace("/\r\n|\r|\n/", "<br>\n", $r[body])

to ignore lines ending with >
(or: > n spaces )
Thanx in advance,
Marco Snoek

Jul 17 '05 #1
3 2806
On Thu, 11 Mar 2004 20:21:33 +0100, "Marco Snoek"
<_[mps]@[webmind.nl]_Dont_you_dare_send.spam> wrote:
Hope you can help me out with this one..

nl2br($string) converts all the \n to <br \>

.. but ..

Suppose I have a table or other HTML elements in $string..
All the TD> and TR> and FORM> are trailed by <br />

How can I adjust the function:
preg_replace("/\r\n|\r|\n/", "<br>\n", $r[body])

to ignore lines ending with >
(or: > n spaces )


I think you're heading into trouble with this approach; what about the
following perfectly valid HTML:

<form method="get"
action="something.php"
name="whatever">

You've now got a whole load of other exceptions to deal with.

The idea of nl2br is to convert _non-HTML_ text data containing newlines to an
HTML equivalent that has <br> elements so that it has newlines in the same
place when inserted into an HTML document.

In general, you can't parse HTML with a single regular expression, as regular
expressions aren't capable of maintaining states as necessary to implement a
HTML parser.

--
Andy Hassall <an**@andyh.co.uk> / Space: disk usage analysis tool
<http://www.andyh.co.uk> / <http://www.andyhsoftware.co.uk/space>
Jul 17 '05 #2
You're absolutely right..
But....
I'm quite sure the trick should work.. I enter the input myself :-)

Can you think of a reg_exp ???

Regards...

Marco
"Andy Hassall" <an**@andyh.co.uk> schreef in bericht
news:8j********************************@4ax.com...
On Thu, 11 Mar 2004 20:21:33 +0100, "Marco Snoek"
<_[mps]@[webmind.nl]_Dont_you_dare_send.spam> wrote:
Hope you can help me out with this one..

nl2br($string) converts all the \n to <br \>

.. but ..

Suppose I have a table or other HTML elements in $string..
All the TD> and TR> and FORM> are trailed by <br />

How can I adjust the function:
preg_replace("/\r\n|\r|\n/", "<br>\n", $r[body])

to ignore lines ending with >
(or: > n spaces )
I think you're heading into trouble with this approach; what about the
following perfectly valid HTML:

<form method="get"
action="something.php"
name="whatever">

You've now got a whole load of other exceptions to deal with.

The idea of nl2br is to convert _non-HTML_ text data containing newlines

to an HTML equivalent that has <br> elements so that it has newlines in the same
place when inserted into an HTML document.

In general, you can't parse HTML with a single regular expression, as regular expressions aren't capable of maintaining states as necessary to implement a HTML parser.

--
Andy Hassall <an**@andyh.co.uk> / Space: disk usage analysis tool
<http://www.andyh.co.uk> / <http://www.andyhsoftware.co.uk/space>

Jul 17 '05 #3
Marco Snoek wrote:
Can you think of a reg_exp ???


You need the "lookbehind" stuff.

Open this URLi: http://www.php.net/manual/en/pcre.pattern.syntax.php
and search "lookbehind" in that page
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--
Jul 17 '05 #4

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

Similar topics

3
4261
by: Matthew Sims | last post by:
Hey all, So I started using nl2br and love it. But when I convert back from <br> to \n, it produces double spaces instead of single space. Converting \n to <br>: $emailBody=nl2br($_POST);...
3
3585
by: NotGiven | last post by:
I have a form that inserts records in a database. When I display records, the paragraphs don't show up. I am using dreamweaver MX, PHP, and MySQL.
11
36033
by: Jakanapes | last post by:
Hi all, I'm looking for a way to scan a block of text and replace all the double quotes (") with single quotes ('). I'm using PHP to pull text out of a mySQL table and then feed the text into...
3
3137
by: ribchr00 | last post by:
Hi all, I would like to replace line breaks such '+' with '<br />'. Easy task. Problems start when I try to only replace lines that do not end with HTML tags. I tried preg_replace("/+/", "<br...
13
2159
by: Chris Goldie | last post by:
From an accessibility point of view, is there any advange in using <P> over <br>? eg, whats the difference between these two examples, are they both accessible? Eg. 1 <p>My first...
822
28897
by: Turamnvia Suouriviaskimatta | last post by:
I 'm following various posting in "comp.lang.ada, comp.lang.c++ , comp.realtime, comp.software-eng" groups regarding selection of a programming language of C, C++ or Ada for safety critical...
9
43090
by: Wayne | last post by:
$a = $_POST; # txt_content = This is a<CR><LF>Test $p = str_replace ("%0D%0A", "<br>", $a); That is the above code that I am using, however, it is not picking up the CR/LF from the textarea. I...
2
3345
Ajm113
by: Ajm113 | last post by:
Ok, I want to disable any html tags, but the problem is when I do add in the nl2br function with a htmlentities it displays the tags for the <br>! I even keep seeing rn every time I enter a return in...
2
1570
by: Defacta | last post by:
Hello ! How to delete all the new lines and replace them by <br>, because new lines involves Javacript Error, IE: facts_infos = "In 1972 he was done for murdering a colleague over a matter of...
0
7055
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6920
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7103
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6758
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
4499
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3011
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3003
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1311
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
203
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.