By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,389 Members | 1,989 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,389 IT Pros & Developers. It's quick & easy.

Replace the CR/LF in a textarea by str_replace

P: n/a
$a = $_POST['txt_content']; # 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 have also attempted singular variations
of the CR/LF combination and even reversed the sequence without success.

Is it possible that it may be encoded differently and if so what is it?

Thanks
Wayne.
Jun 6 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Carved in mystic runes upon the very living rock, the last words of
Wayne of comp.lang.php make plain:
$a = $_POST['txt_content']; # 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.


Try $p = str_replace ("\r\n", "<br>", $a);

However, depending on what OS and/or browser your visitors use, a new
line character might be passed as CR, LF or CRLF. I usually do something
like:

$p = ereg_replace("\r\n?" "\n", $a);
$p = str_replace("\n", '<BR>', $p);

The first line makes sure they're all LFs before converting them to BRs.

--
Alan Little
Phorm PHP Form Processor
http://www.phorm.com/
Jun 7 '06 #2

P: n/a

Wayne wrote:
Is it possible that it may be encoded differently and if so what is it?

Why you don't use nl2br function? It will returns string with '<br />'
inserted before all newlines.

--
http://www.immersivelounge.com

Jun 7 '06 #3

P: n/a
lorento wrote:
Wayne wrote:
Is it possible that it may be encoded differently and if so what is it?

Why you don't use nl2br function? It will returns string with '<br />'
inserted before all newlines.


Perhaps because it is incorrect syntax for other than XHTML. Apart from
the fact that IE doesn't support it, other reasons not to use XHTML
except for XML documents

http://www.xml.com/pub/a/2003/03/19/dive-into-xml.html
http://www.spartanicus.utvinternet.ie/no-xhtml.htm
http://www.hixie.ch/advocacy/xhtml
http://lachy.id.au/log/2005/12/xhtml-beginners
Louise
Jun 7 '06 #4

P: n/a
Alan Little wrote:
$p = ereg_replace("\r\n?" "\n", $a);


Using the above, I transposed it to:
$p = ereg_replace ("/\r\n|\n\r|\r|\n/", "<br>", $p);

Thanks for your feedback and response.
Jun 7 '06 #5

P: n/a
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Wayne wrote:
$a = $_POST['txt_content']; # txt_content = This is a<CR><LF>Test
$p = str_replace ("%0D%0A", "<br>", $a);


- From what language did you come from? "%0D%0A" is not a valid syntax to
specify ascii characters in a string. Please have a read:

http://es.php.net/manual/en/language....syntax.double

And, if you want to check for CR-LF, plus CR alone, plus LF alone, you may
want to pass an array to str_replace:

str_replace( array("\r\n","\r","\n") , '<br>' , $subject);

Alternatively:

str_replace( array("\0x0d\0x0a","\0x0d","\0x0a") , '<br>' , $subject);

Alternatively:

str_replace( array(chr(13).chr(10) , chr(13) , chr(10)) , '<br>' ,
$subject);

And, most important:

nl2br($subject);

- --
- ----------------------------------
Iván Sánchez Ortega -i-punto-sanchez--arroba-mirame-punto-net

http://acm.asoc.fi.upm.es/~mr/ ; http://acm.asoc.fi.upm.es/~ivan/
MSN:i_*************************@hotmail.com
Jabber:iv*********@jabber.org ; iv*********@kdetalk.net
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (GNU/Linux)

iD8DBQFEhoW/3jcQ2mg3Pc8RAn61AJoD2RxamjK7QTy80onOP9clKHMijgCfVK Qt
uY/ZtXUSqrp9ek/vH5bttk4=
=U92G
-----END PGP SIGNATURE-----
Jun 7 '06 #6

P: n/a
boclair schrieb:
Why you don't use nl2br function? It will returns string with '<br />'
inserted before all newlines.

Perhaps because it is incorrect syntax for other than XHTML. Apart from
the fact that IE doesn't support it, other reasons not to use XHTML
except for XML documents


I never heard about a case where <br /> did cause any problem... Anyway
this is still easier than regexps:

str_replace('<br />', '<br>', nl2br($text));

--
Markus
Jun 7 '06 #7

P: n/a
Markus Ernst:
I never heard about a case where <br /> did cause any problem...


I can think of a couple. For example, validation. <br /> means
something different in HTML than it does in XHTML. In HTML, '/' closes
the tag and '>' is treated literally. Not a big problem, maybe, but
probably not what you meant.

The point, I think, isn't that you are or aren't likely to run into
any problems, but rather that for interoperability's sake you should
follow what the specs say - if not by the letter then at least in
spirit.

--
Jock

Jun 9 '06 #8

P: n/a
Message-ID: <11**********************@u72g2000cwu.googlegroups .com> from
John Dunlop contained the following:
I never heard about a case where <br /> did cause any problem...


I can think of a couple. For example, validation. <br /> means
something different in HTML than it does in XHTML. In HTML, '/' closes
the tag and '>' is treated literally. Not a big problem, maybe, but
probably not what you meant.


It is annoying though that nl2br() only returns <br /> Would it be so
difficult to include a switch so that it could output <br> ?

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jun 9 '06 #9

P: n/a
Geoff Berrow schrieb:
Message-ID: <11**********************@u72g2000cwu.googlegroups .com> from
John Dunlop contained the following:

I never heard about a case where <br /> did cause any problem...


I can think of a couple. For example, validation. <br /> means
something different in HTML than it does in XHTML. In HTML, '/' closes
the tag and '>' is treated literally. Not a big problem, maybe, but
probably not what you meant.

It is annoying though that nl2br() only returns <br /> Would it be so
difficult to include a switch so that it could output <br> ?

I agree with both your points - in some cases I just prefer to go the
easy way as long as it can be expected to be supported by browsers for
the next few decades...

Actually it might be a good idea to introduce an ini setting or a
constant with the HTML version to generate for all kinds of HTML
generating functions.

Of course this will generate problems where text is post-processed, for
example double line breaks are changed to new paragraphs:

$text = nl2br($text);
$text = '<p>'.str_replace('<br /><br />', '</p><p>', $text).'</p>';

The str_replace needle will have be changed to nl2br("\n\n").

--
Markus
Jun 12 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.