/    Sign up×
Community /Pin to ProfileBookmark

Hi

I have a form that writes to a php file, and interprets the textarea page breaks as <br>. It also reads from the php file and displays the content in the textarea upon loading the page. What I need to do is have it so when the text is written, it still converts the textarea line breaks to <br> but when it reads from the text file to display it in the text field it converts them back to textarea line breaks.

Here’s my code

[CODE]
<?php

if ($submit) {
$fp = fopen(“iroster.php”, “w”);
fwrite($fp, stripslashes(nl2br($newdata)));
fclose($fp);
}

$fp = fopen(“iroster.php”, “r”);
while (!feof($fp)) {
$data .= fgets($fp, 4096);
}
fclose($fp);
$newdata = nl2br($newdata)
?>

<html>
<head>
<title>Test</title>
</head>

<body>
<form action=”<? print $PHP_SELF; ?>” method=”post”>
<textarea name=”newdata” rows=”10″ cols=”40″>
<?
print $data;
?>
</textarea>

<input type=”submit” name=”submit” value=”Submit”>
</form>

</body>
</html>
[/CODE]

Thanks in advance for any help.

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@phpnoviceApr 07.2005 — Ideally, you want to save the text in a file using the exact same format as when it is displayed in the TEXTAREA. The only time you would "mess" with the text is when you wish to display it as HTML [b][u][i]outside[/i][/u][/b] of the TEXTAREA. There is a standard PHP function for performing this conversion to HTML. (I'll have to look it up this evening.)
Copy linkTweet thisAlerts:
@l3lahauthorApr 13.2005 — I am already having the text formatted to html outside of the text area, but my problem is that the script is reading the text out of the php file and posting it in the textarea. When it is posted, it is posted without <br /> lines and all textarea line breaks are converted to <br />.

So what I'm trying to do is have the script convert them back to normal textarea line breaks so the <br /> is not shown in the textarea when it reads from the text file it writes to.

I know that probably dosent make much sense... But if you would like to see it in action, I'll get a link up to show you.
Copy linkTweet thisAlerts:
@che_rishApr 13.2005 — Yes, can you post the link here so we can see? Thanks. ?
Copy linkTweet thisAlerts:
@che_rishApr 13.2005 — Okay. This how i see your code:

You used the nl2br() function (rough translation: [B]n[/B]ew[B]l[/B]ine 2 [B]br[/B]eak) when writing to a file. You want to undo this, meaning change the <br /> back to n when printing inside the textarea. Correct?

This might help:

Before printing $data inside the <textarea> tag, use str_replace() to replace break with newlines.

[code=php]$data = str_replace("<br />", "n", $data);[/code]

You may want to [URL=http://www.php.net/nl2br/]look at this link for more information[/URL]. The first example there is pretty interesting/helpful.

Please let me know if it worked. ?
Copy linkTweet thisAlerts:
@l3lahauthorApr 13.2005 — That works perfectly except that it seems to double space the text in the textarea. This is no big deal as it still outputs everything perfectly.

Thank you very much for your help.

Edit:

Actually every time I hit submit it not only adds a linebreak to the text area, but adds another line break to the output ?
Copy linkTweet thisAlerts:
@NogDogApr 13.2005 — Probably the text already has a newline after the BR, so try:
[code=php]
$data = str_replace("<br />", "", $data);
[/code]
Copy linkTweet thisAlerts:
@che_rishApr 13.2005 — Hmm. You may want to try this:
[code=php]
$data = str_replace("rn", "n", $data); // make from windows-returns, *nix-returns
$data = str_replace("<br />n", "n", $data); // to retrieve it
[/code]
The first line replaces any carriage-retrun (r) and newlines (n) w/ just a newline.

The second line replaces any breaklines and newlines w/ just a single newline.

[I]Note: I applied the example found in that link i gave you in my last post. [/I]

Hope that works. Im glad to help. ?
×

Success!

Help @l3lah spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.6,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...