Yves Posted July 4, 2008 Share Posted July 4, 2008 Hi, I can't seem to get linebreaks in a textarea. Here's an example string $string = "My name is Yves.\r\n\r\nHere's another line of text." When I echo this line into a textarea, it prints the exact same string. No linebreaks visible in the textarea. Just "My name is Yves.\r\n\r\nHere's another line of text." (without quotes) So, I tried to use nl2br http://be.php.net/nl2br to change \r\n parts of a text string to <br /> 's. However, when I load the page with the textarea again I now see "My name is Yves.<br />Here's another line of text." (without quotes) printed IN the textarea. Again, no linebreaks visible in the textarea. How can I solve this? Tell me if me question seems unclear to you. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/113232-solved-nor-rn-nor-ltbr-gt-please/ Share on other sites More sharing options...
kenrbnsn Posted July 4, 2008 Share Posted July 4, 2008 Please show us your script between tags. Ken Quote Link to comment https://forums.phpfreaks.com/topic/113232-solved-nor-rn-nor-ltbr-gt-please/#findComment-581698 Share on other sites More sharing options...
Yves Posted July 4, 2008 Author Share Posted July 4, 2008 Site uses smarty. in my php file: <?php $query = "SELECT * FROM videos WHERE VIDEOID='".mysql_real_escape_string($VIDEOID)."'"; $executequery = $conn->execute($query); $videoarray = $executequery->getarray(); STemplate::assign('videoarray',$videoarray); ?> in the corresponding tpl file: <textarea name="description" id="description" rows="10" cols="70">{$videoarray[0].description}</textarea><br> Quote Link to comment https://forums.phpfreaks.com/topic/113232-solved-nor-rn-nor-ltbr-gt-please/#findComment-581702 Share on other sites More sharing options...
Yves Posted July 4, 2008 Author Share Posted July 4, 2008 Here's post number 1 again... ---------------------------- Hi, I can't seem to get linebreaks in a textarea. Here's an example string $string = "My name is Yves.\r\n\r\nHere's another line of text." When I echo this line into a textarea, it prints the exact same string. No linebreaks visible in the textarea. Just My name is Yves.\r\n\r\nHere's another line of text. So, I tried to use nl2br http://be.php.net/nl2br to change \r\n parts of a text string to <br /> 's. However, when I load the page with the textarea again I now see My name is Yves.<br />Here's another line of text. printed IN the textarea. Again, no linebreaks visible in the textarea. The br gets written. How can I solve this? Tell me if me question seems unclear to you. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/113232-solved-nor-rn-nor-ltbr-gt-please/#findComment-581706 Share on other sites More sharing options...
teynon Posted July 4, 2008 Share Posted July 4, 2008 If you want a new line in a text area, you need to leave the \n\r there. <br> will only work outside of the textarea. Quote Link to comment https://forums.phpfreaks.com/topic/113232-solved-nor-rn-nor-ltbr-gt-please/#findComment-581711 Share on other sites More sharing options...
SharkBait Posted July 4, 2008 Share Posted July 4, 2008 Try using just \n \r\n is for windows based environments I believe where \n is for *nix environments. Quote Link to comment https://forums.phpfreaks.com/topic/113232-solved-nor-rn-nor-ltbr-gt-please/#findComment-581712 Share on other sites More sharing options...
Yves Posted July 4, 2008 Author Share Posted July 4, 2008 teynon Yes. I need it to work in the textarea. That's the problem. :-\ SharkBait Replacing the \r with \n just prints "\n\n\n\n" instead of "\r\n\r\n" They are still not interpreted as real linebreaks. At least not in a textarea. :-\ Quote Link to comment https://forums.phpfreaks.com/topic/113232-solved-nor-rn-nor-ltbr-gt-please/#findComment-581776 Share on other sites More sharing options...
kenrbnsn Posted July 4, 2008 Share Posted July 4, 2008 Please show us all of your code. Ken Quote Link to comment https://forums.phpfreaks.com/topic/113232-solved-nor-rn-nor-ltbr-gt-please/#findComment-581790 Share on other sites More sharing options...
Yves Posted July 4, 2008 Author Share Posted July 4, 2008 http://www.carbtube.com/myvideosedit_php.txt http://www.carbtube.com/myvideosedit_tpl.txt Quote Link to comment https://forums.phpfreaks.com/topic/113232-solved-nor-rn-nor-ltbr-gt-please/#findComment-581793 Share on other sites More sharing options...
teynon Posted July 4, 2008 Share Posted July 4, 2008 I don't see where you doing any conversion with \n... This works fine for me: <?php echo "<textarea>test\ntest</textarea>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/113232-solved-nor-rn-nor-ltbr-gt-please/#findComment-581803 Share on other sites More sharing options...
Yves Posted July 4, 2008 Author Share Posted July 4, 2008 When I'm converting the \r and \n I'm replaceing this: {$videoarray[0].description} with this: {insert name=transform_to_textarea_description value=var description=$videoarray[0].description} and this is the function supporting it: <?php function insert_transform_to_textarea_description($var) { $textareadescription = $var[description]; $textareadescription = str_replace('\r','\n',$textareadescription); echo "$textareadescription"; } ?> When the string is this: $videoarray[0].description = "My name is Yves.\r\n\r\nHere's another line of text." It outputs this (in the textarea): <textarea>My name is Yves.\n\n\n\nHere's another line of text.</textarea> But, the "\n\n\n\n" are displayed in the textarea. Readable. Not actual linebreaks. Also, when I make a simple test.html with this in it: <textarea>test\ntest</textarea> and load, it shows the \n too. Quote Link to comment https://forums.phpfreaks.com/topic/113232-solved-nor-rn-nor-ltbr-gt-please/#findComment-581812 Share on other sites More sharing options...
wildteen88 Posted July 4, 2008 Share Posted July 4, 2008 When using \r or \n make sure you use double quotes and not single quotes. I swap this line: $textareadescription = str_replace('\r','\n',$textareadescription); for $textareadescription = str_replace(array("\r", "\r\n"), "\n", $textareadescription); That will standardise the new line characters (to just \n). You do not need to use OS specific new line characters in a web page. The web browser should handle the new lines for you. Quote Link to comment https://forums.phpfreaks.com/topic/113232-solved-nor-rn-nor-ltbr-gt-please/#findComment-581823 Share on other sites More sharing options...
teynon Posted July 4, 2008 Share Posted July 4, 2008 Thats because in HTML if your write \n it is two characters. PHP converts the string \n to an "invisible character" that is that of a new line. Do a test.php with the code I sent, bet it will work. Your other problem is that your str_replace code is using single quotes around the characters... I bet you if you change that to double quotes it will do the conversion. str_replace("\r", "\n", $textareadescription); Quote Link to comment https://forums.phpfreaks.com/topic/113232-solved-nor-rn-nor-ltbr-gt-please/#findComment-581826 Share on other sites More sharing options...
corbin Posted July 4, 2008 Share Posted July 4, 2008 I think what's happening is, your database output is literally \r\n, not what they stand for. (Would be represented in PHP as \\r\\n.) So, try doing this: echo str_replace(array("\\r", "\\n"), array("\r", "\n"), $some_var); //cept don't echo... use your templating and what not, but you get the idea (Also, you could use single quotes like '\r' instead of "\\r") Quote Link to comment https://forums.phpfreaks.com/topic/113232-solved-nor-rn-nor-ltbr-gt-please/#findComment-581827 Share on other sites More sharing options...
compguru910 Posted July 4, 2008 Share Posted July 4, 2008 I may be wrong but shouldnt it be \n\r as in new line, return for windows based machines, where as *nix machines are just \n and they automatically follow through with the return? Quote Link to comment https://forums.phpfreaks.com/topic/113232-solved-nor-rn-nor-ltbr-gt-please/#findComment-581828 Share on other sites More sharing options...
wildteen88 Posted July 4, 2008 Share Posted July 4, 2008 I may be wrong but shouldnt it be \n\r as in new line, return for windows based machines, where as *nix machines are just \n and they automatically follow through with the return? Its \r\n for Windows. But anyway I said before (which I think everyone missed: You do not need to use OS specific new line characters in a web page. The web browser should handle the new lines for you. Quote Link to comment https://forums.phpfreaks.com/topic/113232-solved-nor-rn-nor-ltbr-gt-please/#findComment-581831 Share on other sites More sharing options...
corbin Posted July 4, 2008 Share Posted July 4, 2008 Yeah, but he needs to put out some kind of new line, whether it's \n or \r\n. My example would work with either, assuming that's what his problem was. Quote Link to comment https://forums.phpfreaks.com/topic/113232-solved-nor-rn-nor-ltbr-gt-please/#findComment-581836 Share on other sites More sharing options...
Yves Posted July 4, 2008 Author Share Posted July 4, 2008 Well, I didn't quite understand what you posted, corbin, but it seems you provided the solution! The database output is literally \r\n (or \r\n\r\n when 2 linebreaks where entered). So I did't know why all the above didn't work... but I will try to remember this ... situation (?). I cannot thank all of you guys enough. But I CAN thank all equally!!! You've all been a great help! Thanks!!! PS: Sorry for being gone for a while. Yves I think what's happening is, your database output is literally \r\n, not what they stand for. (Would be represented in PHP as \\r\\n.) So, try doing this: echo str_replace(array("\\r", "\\n"), array("\r", "\n"), $some_var); //cept don't echo... use your templating and what not, but you get the idea (Also, you could use single quotes like '\r' instead of "\\r") Quote Link to comment https://forums.phpfreaks.com/topic/113232-solved-nor-rn-nor-ltbr-gt-please/#findComment-581876 Share on other sites More sharing options...
Yves Posted July 4, 2008 Author Share Posted July 4, 2008 I think I know what you meant, corbin. It repected the way the \r and \n where written in the database output. So, what should of been in the database output all along for \r and \n? Some html encoded characters? Quote Link to comment https://forums.phpfreaks.com/topic/113232-solved-nor-rn-nor-ltbr-gt-please/#findComment-581882 Share on other sites More sharing options...
corbin Posted July 4, 2008 Share Posted July 4, 2008 Well, when ever you do the following in PHP, it replaces it with a line break. $str = "Hi\nthere was a break!"; But, the \n is simply an easier way of writing a line break. (The \n stands for numeric code 10 in ASCII, which is a new line, if you want to get technical.) In other words, the \n is simply a way of making it so that: $str = "You\ncan\ndo\nthis\nto\ngo\nnew\nlines."; $str = "Instead of doing this."; But, when ever PHP receives the string from MySQL, it does not parse the \n's or \r's, it simply thinks they mean exactly that, the character \, followed by the character n or r. So what the str_replace did, was it replaced the literal backslash followed by an n or r with what it represents, basically forcing PHP to parse the \r's and \n's. Quote Link to comment https://forums.phpfreaks.com/topic/113232-solved-nor-rn-nor-ltbr-gt-please/#findComment-581897 Share on other sites More sharing options...
Yves Posted July 4, 2008 Author Share Posted July 4, 2008 Right. So the \r's and \n's will always be put into MySQL fields like \r's and \n's, but will have to be transformed once you want them to come out again, sort of, to parse as real \r's and \n's. Thanks for your elaboration. Quote Link to comment https://forums.phpfreaks.com/topic/113232-solved-nor-rn-nor-ltbr-gt-please/#findComment-581902 Share on other sites More sharing options...
corbin Posted July 4, 2008 Share Posted July 4, 2008 So the \r's and \n's will always be put into MySQL fields like \r's and \n's No. For some reason or other, they're being put into MySQL literally.... In PHP it would look like: "INSERT INTO some_table VALUES ('This is a value \\n with a line break.');" Why an actual line break is not being inserted instead, I'm not quite sure. Quote Link to comment https://forums.phpfreaks.com/topic/113232-solved-nor-rn-nor-ltbr-gt-please/#findComment-581911 Share on other sites More sharing options...
Yves Posted July 4, 2008 Author Share Posted July 4, 2008 Well, it beats me too. Anyway, I'm glad it's sorted out. That description area really looked like a mess. I wouldn't want first few members to complain about that. How do you like the site btw? Any suggestions/comments/remarks? :-X Quote Link to comment https://forums.phpfreaks.com/topic/113232-solved-nor-rn-nor-ltbr-gt-please/#findComment-581917 Share on other sites More sharing options...
corbin Posted July 4, 2008 Share Posted July 4, 2008 Guessing the site in your sig? It won't load.... lol Guess it's either down or going slow. Quote Link to comment https://forums.phpfreaks.com/topic/113232-solved-nor-rn-nor-ltbr-gt-please/#findComment-581920 Share on other sites More sharing options...
Yves Posted July 4, 2008 Author Share Posted July 4, 2008 whad up? jk Well, I've noticed these little timeouts for little over a week now and it's really getting to me. I can't stay with this host. It's too embarassing. Eventhough it only started happening at beginning of this week and especially when my host's site never ever times out... Any specific host you'd recommend? Quote Link to comment https://forums.phpfreaks.com/topic/113232-solved-nor-rn-nor-ltbr-gt-please/#findComment-581923 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.