468,544 Members | 1,749 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,544 developers. It's quick & easy.

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 2599
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Matthew Sims | last post: by
11 posts views Thread by Jakanapes | last post: by
3 posts views Thread by ribchr00 | last post: by
13 posts views Thread by Chris Goldie | last post: by
822 posts views Thread by Turamnvia Suouriviaskimatta | last post: by
9 posts views Thread by Wayne | last post: by
2 posts views Thread by Defacta | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by UniDue | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.