472,971 Members | 2,157 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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 2777
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
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
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
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
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
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
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
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
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
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
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
3
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.