Schlo_50 Posted May 22, 2007 Share Posted May 22, 2007 hello, I am trying to create a simple text editor that when used adds the text to a .txt file. At the moment im having trouble with the formatting of the text in terms of new lines. If i write in one long sentence the positioning of the text is fine however if i try to use paragraphs extra lines are added in and then the code gets confused and makes parts of the paragraphs titles, shown in bold. I think it may be because of the "\n"; part of my code but im not sure! Please can someone revise this so i can use paragraphs of text succesfully? :Editor Code: <!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 /></strong></span> <?php $prnt = "<img border=\"0\" src=\"images/bold.jpg\" width=\"25\" height=\"25\" alt=\"Bold\" onClick=\"selectit('<b>Text Here</b>')\"> "; $prnt .= "<img border=\"0\" src=\"images/underline.jpg\" width=\"25\" height=\"25\" alt=\"Underline\" onClick=\"selectit('<you>Text Here</you>')\"> "; $prnt .= "<img border=\"0\" src=\"images/link.jpg\" width=\"25\" height=\"25\" alt=\"Hyperlink\" onClick=\"selectit('<url>http://www.site.com>Site Name</url>')\"> "; ?> <?php echo("$prnt"); ?> <?php if ($_POST['Submit12'] == "Submit") { $file_name = "home.txt"; $open_file = fopen($file_name, "a+"); $file_contents= $_POST['title'] . '|' . $_POST['news'] ."\n"; 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" /> <p> <textarea name="news" cols="85" rows="15" id="news"> </textarea> </p> <p> <label> <input type="submit" name="Submit12" value="Submit" /> </label> </p> </form> </label> :This is how i display the .txt file: <body> Testing Text Formatting.. <br /> <br /> <br /> <?php $file = file("home.txt"); foreach($file as $key => $val){ $data[$key] = explode("|", $val); $title = $data[$key][0]; $news = $data[$key][1]; $outa = "<b><you>$title</you></b><br><br>"; $outb = "$news<br>"; print( $outa ); print( $outb ); } ?> </body> </html> Thanks in advance if anyone can help me with this one! Quote Link to comment https://forums.phpfreaks.com/topic/52573-text-editor/ Share on other sites More sharing options...
corbin Posted May 22, 2007 Share Posted May 22, 2007 The problem is the way you're storing it in the file.... When \n is put into the text file it makes a new line.... Later when you read the script, it explode |, but the content is on a new line.... This causes php to think it's blank or a title if it's a new line ;p. Quote Link to comment https://forums.phpfreaks.com/topic/52573-text-editor/#findComment-259412 Share on other sites More sharing options...
Schlo_50 Posted May 22, 2007 Author Share Posted May 22, 2007 So what do i remove to stop the new lines from happening? The explode '|' or "\n" ? Quote Link to comment https://forums.phpfreaks.com/topic/52573-text-editor/#findComment-259423 Share on other sites More sharing options...
Schlo_50 Posted May 22, 2007 Author Share Posted May 22, 2007 I really need to know how i stop a extra lines from being created when a user hits enter for a new paragraph! Please help! Quote Link to comment https://forums.phpfreaks.com/topic/52573-text-editor/#findComment-259438 Share on other sites More sharing options...
HaLo2FrEeEk Posted May 22, 2007 Share Posted May 22, 2007 $text = str_replace("\n", "|", $text); Try that. Quote Link to comment https://forums.phpfreaks.com/topic/52573-text-editor/#findComment-259439 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.