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?
(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
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
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
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.
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
> 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
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...
That's EXACTLY what I had changed it to. It's working fine now.
preg_replace('/\r?\n|\r/', '</p><p>', $Content);
--
regards,
reggie.
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...