Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Str_replace And New Lines

3 views
Skip to first unread message

vKp

unread,
Jan 26, 2003, 2:11:00 PM1/26/03
to
I have this piece of code:
function nl2p($Content) {
$Content = str_replace("\n", "</p><p>", $Content);
return $Content;
}

Which takes in information from a textarea as a string. It is adding in the
</p><p> as required, but it isn't removing the new line. Any suggestions?


Chris Morris

unread,
Jan 26, 2003, 2:31:52 PM1/26/03
to

(especially) If the browser is running in windows, it may be submitting \r\n
instead of \n as the newline marker. Other browsers might submit \r
alone.

Try str_replace()ing \r\n with \n, then \r with \n, then \n with
</p></p>
(I think that's the right order to work for all three newline markers)

--
Chris

André Næss

unread,
Jan 26, 2003, 4:12:44 PM1/26/03
to
vKp wrote:

As has allready been noted, different platforms represent the newline
differently:
Mac: \r
Windows: \r\n
Unix: \n

Replacing a newline with <p> is wrong IMO as paragraphs in computer text is
usually represented as two newlines, that is, the user sees an empty line.
With this in mind you will have to improve your routine somewhat. My
suggestion is to split the text on every double newline, (albeit allowing
for the possibility of other whitespace chars between the two newlines),
and regard each piece as a paragraph. When this is done you can replace any
remaining newlines using nl2br(), which automatically handles all the
different newline representations.

André Næss

vKp

unread,
Jan 26, 2003, 3:51:45 PM1/26/03
to
"André Næss" <andre.ha...@ifi.uio.no> wrote in message
news:b11fft$s8f$1...@maud.ifi.uio.no...

> As has allready been noted, different platforms represent the newline
> differently:
> Mac: \r
> Windows: \r\n
> Unix: \n
>
> Replacing a newline with <p> is wrong IMO as paragraphs in computer text
is
> usually represented as two newlines, that is, the user sees an empty line.
> With this in mind you will have to improve your routine somewhat. My
> suggestion is to split the text on every double newline, (albeit allowing
> for the possibility of other whitespace chars between the two newlines),
> and regard each piece as a paragraph. When this is done you can replace
any
> remaining newlines using nl2br(), which automatically handles all the
> different newline representations.
>
Thanks to you and Chris for the help (although I haven't had a chance to try
it out yet). But I disagree on the paragraphing. The reason I wrote a
function called nl2p() was to get away from the semantically devoid nl2br().
While it would be going a fair way off topic to discuss this further, I
suggest reading "Bed and BReakfast mark-up"
(http://www.tantek.com/log/2002/10.html).


André Næss

unread,
Jan 26, 2003, 4:08:09 PM1/26/03
to
vKp wrote:

I don't think you quite understood me. Of course you should represent
paragraphs with <p></p>. The point is that your code replaces a *single*
newline with a paragraph, so the following text:

This is a line broken
once by the user

Will be turned into:

This is a line broken</p><p>once by the user

Which certainly isn't valid HTML! Besides, it's not what the user expects.
Consider this example:

This time we want paragraphs

This is the second paragraph

Which your code will turn into:

This time we want paragraphs</p><p></p><p>This is the second paragraph

What you want is of course:

<p>This time we want paragraphs</p>
<p>This is the second paragraph</p>

(And in the first example you really *want* a <br/>.)

I hope this clarifies matters, and I believe my previously proposed solution
should be adequate.

André Næss


vKp

unread,
Jan 26, 2003, 5:03:32 PM1/26/03
to
"André Næss" <andre.ha...@ifi.uio.no> wrote in message
news:b11inr$sdd$1...@maud.ifi.uio.no...

> I don't think you quite understood me. Of course you should represent
> paragraphs with <p></p>. The point is that your code replaces a *single*
> newline with a paragraph, so the following text:
>
> This is a line broken
> once by the user
>
> Will be turned into:
>
> This is a line broken</p><p>once by the user
>
> Which certainly isn't valid HTML! Besides, it's not what the user expects.
> Consider this example:
>
> This time we want paragraphs
>
> This is the second paragraph
>
> Which your code will turn into:
>
> This time we want paragraphs</p><p></p><p>This is the second paragraph
>
> What you want is of course:
>
> <p>This time we want paragraphs</p>
> <p>This is the second paragraph</p>
>
> (And in the first example you really *want* a <br/>.)
>

I understand what you're getting at, but I think that is because I haven't
shown the full piece of code (this is only one function).
The textarea text is automatically wrapped in a <p></p> so that example 1 is
valid HTML (<p>This is a line broken</p><p>once by the user</p>). Any empty
paragraphs are removed giving example 2 the right output as well.
And I really don't want <br /> in example 1. I want the output to be valid
and semantically rich XHTML. <br /> is pretty meaningless and only serves
presentation. Hence it has no purpose in modern mark-up (especially that of
text). If a user (of which I am currently the only one) decides to break
this, it's easy enough to manually add a <br />. As of yet, there's been no
reason to.

Nikolai Chuvakhin

unread,
Jan 26, 2003, 5:54:08 PM1/26/03
to
"vKp" <ass...@hotmail.com> wrote in message
news:<b11brv$j6e$1...@newsg1.svr.pol.co.uk>...

Remember that lines end with \n only on *nix. On Windows, it's \r\n,
on Mac, \r. So your function should probably look like this:

function nl2p($Content) {
$Content = str_replace("\r\n", "\n", $Content);
$Content = str_replace("\r", "\n", $Content);


$Content = str_replace("\n", "</p><p>", $Content);
return $Content;
}

Cheers,
NC

André Næss

unread,
Jan 27, 2003, 2:21:41 AM1/27/03
to
vKp wrote:

> I understand what you're getting at, but I think that is because I haven't
> shown the full piece of code (this is only one function).
> The textarea text is automatically wrapped in a <p></p> so that example 1
> is valid HTML (<p>This is a line broken</p><p>once by the user</p>). Any
> empty paragraphs are removed giving example 2 the right output as well.
> And I really don't want <br /> in example 1. I want the output to be valid
> and semantically rich XHTML. <br /> is pretty meaningless and only serves
> presentation. Hence it has no purpose in modern mark-up (especially that
> of text). If a user (of which I am currently the only one) decides to
> break this, it's easy enough to manually add a <br />. As of yet, there's
> been no reason to.

Well, I'd still say that you should rewrite your function. It's not very
clever to have a function which relies on the effects of some other
function. You can't put your current nl2p into a library and reuse it
whenever you want, you'll have to wrap it's output in <p></p> every time
you use it.

I'd write it so that it can perform the nl2p operation entirely by itself,
simply because that's a cleaner design.

André Næss

vKp

unread,
Jan 27, 2003, 12:06:07 PM1/27/03
to
"André Næss" <andre.ha...@ifi.uio.no> wrote in message
news:b12mm6$1sh$1...@maud.ifi.uio.no...

> Well, I'd still say that you should rewrite your function. It's not very
> clever to have a function which relies on the effects of some other
> function. You can't put your current nl2p into a library and reuse it
> whenever you want, you'll have to wrap it's output in <p></p> every time
> you use it.
>
> I'd write it so that it can perform the nl2p operation entirely by itself,
> simply because that's a cleaner design.

You're probably right on that account. It was done as a private function as
part of a package. (I'd been doing a lot of ADA the week before and the
style just stuck). Also, I wanted the wrapping function to be separate (too
complicated to go into).
As long as it's working for now...


vKp

unread,
Jan 27, 2003, 12:06:23 PM1/27/03
to

"Nikolai Chuvakhin" <n...@iname.com> wrote in message
news:32d7a63c.03012...@posting.google.com...

> Remember that lines end with \n only on *nix. On Windows, it's \r\n,
> on Mac, \r. So your function should probably look like this:
>
> function nl2p($Content) {
> $Content = str_replace("\r\n", "\n", $Content);
> $Content = str_replace("\r", "\n", $Content);
> $Content = str_replace("\n", "</p><p>", $Content);
> return $Content;
> }
>
> Cheers,
> NC

That's EXACTLY what I had changed it to. It's working fine now.


reggie

unread,
Jan 27, 2003, 2:01:30 PM1/27/03
to


preg_replace('/\r?\n|\r/', '</p><p>', $Content);


--
regards,
reggie.

Randell D.

unread,
Jan 27, 2003, 4:08:40 PM1/27/03
to

"vKp" <ass...@hotmail.com> wrote in message
news:b11brv$j6e$1...@newsg1.svr.pol.co.uk...

This might be environmentally unfriendly to your system, but have you tried
using implode/explode on your text input, using the new line as a field
seperator, then convert it back to a string, replacing your new-line with
some other character(s) like <p></p>? In addition, perform trim to shave
the sides...

Thats my two-pence worth - though don't blame me if Green Peace come
knocking on your door saying that usage of implode/explode are wasteing
resources...


0 new messages