Page 1 of 1

Line breaks problem :(

Posted: Mon Oct 30, 2006 4:55 pm
by ykarmi
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi guys, I wrote a code that reads what a user typed in a textarea created via HTML and creates a new file called yourfile.js with the text that the user typed in the textarea. My only problem is that when the user hits the return key on the keyboard (AKA enter) the new file contains moves to the next line. I can't have that because the a variable is being loaded from javascript from that file, and java script cannot have more than one continuous line in the var. I want to convert the new lines (enters) to br, /n or something like that, so far unsuccessfully. This is my HTML code with my textarea:
[syntax="html"]
<html>
<body>
<form action="form.php" method="post">
<textarea name="info" wrap="off"></textarea>
<input type="submit">
</form>
</body>
</html>
This is my PHP code:[/syntax]

Code: Select all

<html>
<body>
<?php

$myFile = "yourfile.js";
$information = $_POST['info'];
$fh = fopen($myFile, 'w') or die("can't open file");
$information = nl2br($information);
$information = str_replace("<br />","/n", $information);
fwrite($fh, $information);
fclose($fh);

?>

</body>
</html>
Thank you so much in advanced!!! :D

p.s.
The breaks are added, I see /n at the end of every line, but the enters still exist in the file created alongside to the /n's

EDIT: PLEASE NOTE I -DID- USE nl2br BUT IT DIDN'T WORK AS EXPECTED


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Mon Oct 30, 2006 5:05 pm
by hawleyjr
Hello ykarmi, I'm sure your post is urgent but please don't put that in the subject....


nl2br()

OK

Posted: Mon Oct 30, 2006 5:34 pm
by ykarmi
Thank you for your comment - I edited the message so the "URGENT" does not appear anymore - although it still is.
Please take a look at my code, I did use ln2br there but it did not work as expected.
Thank you very much
Yuval

Re: OK

Posted: Mon Oct 30, 2006 5:37 pm
by hawleyjr
ykarmi wrote:Thank you for your comment - I edited the message so the "URGENT" does not appear anymore - although it still is.
Please take a look at my code, I did use ln2br there but it did not work as expected.
Thank you very much
Yuval
You are using nl2br() sorry I missed that.

Check out the manual for str_replace()

Code: Select all

mixed str_replace ( mixed search, mixed replace, mixed subject [, int &count] )

Code: Select all

$information = str_replace("<br />","/n", $information);

I'm sorry

Posted: Mon Oct 30, 2006 5:43 pm
by ykarmi
I'm sorry, i still don't understand what the problem is...

Posted: Mon Oct 30, 2006 5:55 pm
by volka
$information = str_replace("<br />","/n", $information);
means: replace <br /> by \n
that's more or less the reverse operation of nl2br.

OK

Posted: Mon Oct 30, 2006 6:06 pm
by ykarmi
I realized what my problem is!
It's not even PHP, it's actually javascript.
I need to load a .js file to a certain document to retrieve a variable,
but there are line breaks after the var= thing. This is how it looks:

Code: Select all

myText='<h5><center>this<br />
is a<br />
test</center></h5>'
I need it to look like this

Code: Select all

myText='<h5><center>this<br /> is a<br /> test</center></h5>'
or else the js wouldn't read it and give me an undefined value for the variable myText.
Is there any way i can force the js to read myText with the line breaks?
Or is there a better solution in php to force php write it without the line breaks?
Thank you very much!
Yuval :D