Buyocat Posted June 11, 2006 Share Posted June 11, 2006 I would like to take a chunk of text gathered from a form and replace new lines with </p><p> or something along those lines so that I submitted data can retain its paragraph structure. I know that nl2br does something similar but that's not quite what I want. I tried to eregi_replace('\r\n', '</p><p>', $text) but that didn't work, so I guess what I'd like to know is what symbol am I looking for, or is there another way or material I can look at for guidance? Quote Link to comment https://forums.phpfreaks.com/topic/11753-recognizing-new-lines/ Share on other sites More sharing options...
AndyB Posted June 11, 2006 Share Posted June 11, 2006 Try \n instead of \r\n ... and str_replace is a better choice than ereg(i)_replace according to the manual at [a href=\"http://ca.php.net/manual/en/function.str-replace.php\" target=\"_blank\"]http://ca.php.net/manual/en/function.str-replace.php[/a] Quote Link to comment https://forums.phpfreaks.com/topic/11753-recognizing-new-lines/#findComment-44448 Share on other sites More sharing options...
Buyocat Posted June 11, 2006 Author Share Posted June 11, 2006 I'm sorry I forget to mention it, but I tried that first without any luck. Quote Link to comment https://forums.phpfreaks.com/topic/11753-recognizing-new-lines/#findComment-44449 Share on other sites More sharing options...
poirot Posted June 12, 2006 Share Posted June 12, 2006 Use double quotes or PHP won't "see" that as a newline character.Also I don't recommend you to use <p></p> as replacement, because they would be an empty element... No big deal, but some xhtml/html validators don't like them. Quote Link to comment https://forums.phpfreaks.com/topic/11753-recognizing-new-lines/#findComment-44495 Share on other sites More sharing options...
mainewoods Posted June 12, 2006 Share Posted June 12, 2006 [!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Also I don't recommend you to use <p></p> as replacement,[/quote]-just replace with '</p><p>' not the other way around and then print the extra tags before and after:[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]echo '<p>' . $converted_string . '</p>';[/quote] Quote Link to comment https://forums.phpfreaks.com/topic/11753-recognizing-new-lines/#findComment-44631 Share on other sites More sharing options...
Buyocat Posted June 12, 2006 Author Share Posted June 12, 2006 Well replacing the "\n" with '</p><p>' does work, but as Poirot warned it leads to extra emtpy </p><p> if the user were to say hit enter twice (something that I do, so I can trust others to do). So can anyone think of a decent way to get an xhtml compliant set of p tags around the text but not around whitespace? ps I tried searching for "\n\n" but that didn't work, and I'd rather not have to use an elaborate if statement, nl2br is enough for the job even if it doesn't satisfy completely/ Quote Link to comment https://forums.phpfreaks.com/topic/11753-recognizing-new-lines/#findComment-44722 Share on other sites More sharing options...
effigy Posted June 12, 2006 Share Posted June 12, 2006 Try this:[code]<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"><textarea name="txt"></textarea><br><input type="submit"></form><?php if ($_POST) { $p_txt = trim($_POST['txt']); $p_txt = '<p>' . preg_replace("/(\r\n)+/", '</p><p>', $p_txt). '</p>'; echo '<pre>', htmlentities($p_txt), '</pre>'; }?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/11753-recognizing-new-lines/#findComment-44729 Share on other sites More sharing options...
Buyocat Posted June 12, 2006 Author Share Posted June 12, 2006 Thanks effigy that did the trick. Quote Link to comment https://forums.phpfreaks.com/topic/11753-recognizing-new-lines/#findComment-44737 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.