473,387 Members | 1,791 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Replace the CR/LF in a textarea by str_replace

$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
9 43074
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

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
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
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
-----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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Jon | last post by:
Why does the following not replace the ":)"? str_replace(":)", "&nbsp;<img src=http://www.com.com/forum/emoticons/smiley.gif>&nbsp;", $_POST); But, the following does replace the ":("? ...
1
by: Geoff Berrow | last post by:
I have a textarea on a form whose value is stored in a database. I want to display the contents as an unordered list. At the moment I am using * to start the line and ; to end the line in the...
3
by: Martin Herrman | last post by:
Dear programmers, I have a form: <form action=".." method="post"> <textarea name="text1"></textarea> </form> And a php file that saves this variable to a file:
2
by: windandwaves | last post by:
Hi Folk I am getting some data from an html form (textarea). I am having trouble with replacing line breaks in the data with spaces. here is my line: trim(stripslashes(str_replace("\n",...
12
by: Michael | last post by:
In PHP there is a function called str_replace (http://php.net/str_replace). Basically you can freed in two strings and a "subject" string. Then it goes through the subject string searching for...
4
by: Nate12o6 | last post by:
Mabee you guys can help me with this. I have a form with enctype="multipart/form-data" that has a textarea in it as well as file upload. The text area is for a description of the file. If...
8
luke14free
by: luke14free | last post by:
Hello, I'm creating a form to let user leave comments, I'm having a problem, because I need to convert all the \n to <br/>. After several tries I came out with this simple function <script...
1
by: olddocks | last post by:
i am currently working on a WYSIWYG html editor with javascript. i have to convert textarea to iframe to do get the html environment to get it work as form POST. my questions are.. 1. can...
24
by: Petyr David | last post by:
I've seen a few ideas - figured I'd run it up the flagpole and see if anyone saluted Thanks in advance
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.