Schlo_50 Posted May 30, 2007 Share Posted May 30, 2007 I have a text area where large amounts of text can be added but when ever a carriage return occurs the new line isn't echoed properly, im trying to use str_replace to replcace \n for <br /> but the <br /> will not add to my home.txt file. <?php if ($_POST['Submit12'] == "Submit") { $file_name = "home.txt"; $open_file = fopen($file_name, "a+"); $file_contents= $_POST['title'] . '|' . $_POST['news'] ."\n"; $replace = str_replace("\n", "<br />", $file_contents); fwrite($open_file, $file_contents); fclose($open_file); echo "<meta http-equiv=\"refresh\" content=\"0;URL=homepagetry.php\">"; echo "Form data successfully written to file"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/53602-str_replace-help/ Share on other sites More sharing options...
chigley Posted May 30, 2007 Share Posted May 30, 2007 nl2br() ~ no str_replace() needed! Quote Link to comment https://forums.phpfreaks.com/topic/53602-str_replace-help/#findComment-264949 Share on other sites More sharing options...
rhyspaterson Posted May 30, 2007 Share Posted May 30, 2007 \n = a new line \r = a carriage return \n\r = some kind of super-human ultimate new line carriage return hope that helped Quote Link to comment https://forums.phpfreaks.com/topic/53602-str_replace-help/#findComment-264950 Share on other sites More sharing options...
Schlo_50 Posted May 30, 2007 Author Share Posted May 30, 2007 Warning: Wrong parameter count for nl2br() in C:\apachefriends\xampp\htdocs\rotaryclub\homepagetry.php on line 84 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Untitled Document</title> <style type="text/css"> <!-- body { background-color: #6191c2; } --> </style> <SCRIPT language="Javascript"> function selectit(input){ document.formname.news.value+=input } </SCRIPT> <link href="rotary.css" rel="stylesheet" type="text/css" /> </head> <body> <label> <p><span class="rotary"><strong>Text Editor<br /><br /></strong></span> <?php $a = "<img border=\"0\" src=\"images/bold.jpg\" width=\"25\" height=\"25\" alt=\"Bold\" onClick=\"selectit('<b>Text Here</b>')\"> "; $b .= "<img border=\"0\" src=\"images/underline.jpg\" width=\"25\" height=\"25\" alt=\"Underline\" onClick=\"selectit('<u>Text Here</u>')\"> "; $c .= "<img border=\"0\" src=\"images/link.jpg\" width=\"25\" height=\"25\" alt=\"Hyperlink\" onClick=\"selectit('<url>http://www.site.com>Site Name</url>')\"> "; $d = "<img border=\"0\" src=\"images/para.jpg\" width=\"100\" height=\"25\" alt=\"New Paragraph\" onClick=\"selectit('<br><br>')\"> "; ?><?php if ($_POST['Submit12'] == "Submit") { $file_name = "home.txt"; $open_file = fopen($file_name, "a+"); $file_contents= $_POST['title'] . '|' . $_POST['news'] ."\n"; nl2br("\n\r", "<br />"); fwrite($open_file, $file_contents); fclose($open_file); echo "<meta http-equiv=\"refresh\" content=\"0;URL=homepagetry.php\">"; echo "Form data successfully written to file"; } ?> </p> <form id="formname" name="formname" method="post" action=""> <p> <span class="rotary">Title:</span> <input type="text" name="title" /> <br /><br /><?php echo("$a"); echo("$b"); echo("$c"); echo("$d"); ?> <p> <textarea name="news" cols="50" rows="15" id="news" wrap="hard"></textarea> </p> <p> <label> <input type="submit" name="Submit12" value="Submit" /> </label> </p> </form> </label> <span class="rotary"> <?php $file = file("home.txt"); foreach($file as $key => $val){ $data[$key] = explode("|", $val); $title = $data[$key][0]; $news = $data[$key][1]; $outa = "<br><b><u>$title</u></b><br>"; $outb = "$news<br><br>"; $outc = "____________________________________"; nl2br("\n\r", "<br />"); print( $outa ); print( $outb ); print( $outc ); } ?> </span> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/53602-str_replace-help/#findComment-264983 Share on other sites More sharing options...
kenrbnsn Posted May 30, 2007 Share Posted May 30, 2007 Did you read the manual page for the function nl2br()? You pass it one parameter which is the string containing the newline characters. It will insert the string "<br />" before each newline character and return the modified string. Ken Quote Link to comment https://forums.phpfreaks.com/topic/53602-str_replace-help/#findComment-264985 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.