Jump to content

[SOLVED] Nor \r\n nor <br /> please


Yves

Recommended Posts

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.

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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. :-\

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

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")

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!!!  8)

 

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")

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.