473,769 Members | 2,106 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 43159
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\0x 0a","\0x0d","\0 x0a") , '<br>' , $subject);

Alternatively:

str_replace( array(chr(13).c hr(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*********@kde talk.net
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (GNU/Linux)

iD8DBQFEhoW/3jcQ2mg3Pc8RAn6 1AJoD2RxamjK7QT y80onOP9clKHMij gCfVKQt
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('<b r />', '<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 interoperabilit y'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************ **********@u72g 2000cwu.googleg roups.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************ **********@u72g 2000cwu.googleg roups.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_repla ce('<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
4608
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 ":("? str_replace(":(", "&nbsp;<img src=http://www.com.com/forum/emoticons/crying.gif>&nbsp;", $_POST);
1
2764
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 textarea and then replacing these characters like so: <ul> <?php $features=$myrow; $search = array("*", ";"); $replace =array("\t\t<li>", "</li>\n");
3
2832
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
4410
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", '&nbsp;', (htmlentities($_POST))))));
12
7323
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 occurences of the "search" string and replaces them with the "replace" string. Is there something simular in JavaScript, or can someone give me a solution. I am an experienced PHP user and XHTML writer, and I have learnt Javascript to a reasonable...
4
2579
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 someone uses the enter key to make 2 lines in the text area when it is submitted it is stored in mysql database with (i guess) a line break \n? When i pull the data out of mysql to put back into the textarea i use the javascript function...
8
13169
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 language="jscript"> function repp() { var ss = document.getElementById('text').value; ss = ss.replace(/\r\n/g,"<br />"); document.getElementById('text').value = ss; } </script>
1
1820
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 iframe submit the POST form data to script? is it form object? 2. does anybody know how to replace those textarea with iframe? i am looking to code my own rich text editor which is a text area replacement.
24
3622
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
9423
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10216
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10049
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9997
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9865
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8873
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3965
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3565
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.