Baez Posted June 10, 2010 Share Posted June 10, 2010 I'm submitting a form to another page. Once it gets to the second page, it does a few replacements and displays the form data from the textarea. I can't seem to match newlines properly though. Here's the code: $data = preg_replace("/\n|\r/", "<br/>", $_POST['data']); It matches nothing and displays the newlines as spaces. Link to comment https://forums.phpfreaks.com/topic/204423-replace-newline-characters-with-html-breaks/ Share on other sites More sharing options...
Alex Posted June 10, 2010 Share Posted June 10, 2010 Use nl2br. Link to comment https://forums.phpfreaks.com/topic/204423-replace-newline-characters-with-html-breaks/#findComment-1070502 Share on other sites More sharing options...
Baez Posted June 10, 2010 Author Share Posted June 10, 2010 Thanks. Tried it but it's still displaying as a space and not replacing anything. I used it like so: $data = nl2br($_POST['data']); Example, this is what would be in the textarea field: Form Data And this is what it displays on the next page: Form Data Link to comment https://forums.phpfreaks.com/topic/204423-replace-newline-characters-with-html-breaks/#findComment-1070507 Share on other sites More sharing options...
jonsjava Posted June 10, 2010 Share Posted June 10, 2010 $data = nl2br($_POST['data']); That doesn't work? Link to comment https://forums.phpfreaks.com/topic/204423-replace-newline-characters-with-html-breaks/#findComment-1070508 Share on other sites More sharing options...
Alex Posted June 10, 2010 Share Posted June 10, 2010 Please post your exact code. That should work, but you could be making another mistake; for example, echoing out $_POST['data'] instead of $data. Link to comment https://forums.phpfreaks.com/topic/204423-replace-newline-characters-with-html-breaks/#findComment-1070509 Share on other sites More sharing options...
Baez Posted June 10, 2010 Author Share Posted June 10, 2010 $descrip = nl2br($_SESSION['descriptemp']); $descrip = preg_replace("/\[b\]/", "<strong>", $_SESSION['descriptemp']); $descrip = preg_replace("/\[\/b\]/", "</strong>", $_SESSION['descriptemp']); $pageShowGuide = new Page(); $pageShowGuide->incpath = "../../"; $pageShowGuide->sidebar = ""; $pageShowGuide->title = $_SESSION['titletemp']; $pageShowGuide->content = " <div id=\"wide_main\"> <div id=\"guide_summ_".$_SESSION['colortemp']."\"> <div id=\"guide_pic\"> <img src=\"images/game/".$type."/".$img."\" /> </div> ".$descrip." </div> <h1>".$_SESSION['titletemp']."</h1> </div>"; $pageShowGuide->showPage(); In the page before, the SESSION variables are set from the raw POST data. The reason I'm using a second page is the first determines which submit button was pressed and continues from there. If you need any other code I'll post it up. Link to comment https://forums.phpfreaks.com/topic/204423-replace-newline-characters-with-html-breaks/#findComment-1070513 Share on other sites More sharing options...
jonsjava Posted June 10, 2010 Share Posted June 10, 2010 $descrip = nl2br($_SESSION['descriptemp']); $pageShowGuide = new Page(); $pageShowGuide->incpath = "../../"; $pageShowGuide->sidebar = ""; $pageShowGuide->title = $_SESSION['titletemp']; $pageShowGuide->content = " <div id=\"wide_main\"> <div id=\"guide_summ_".$_SESSION['colortemp']."\"> <div id=\"guide_pic\"> <img src=\"images/game/".$type."/".$img."\" /> </div> ".$descrip." </div> <h1>".$_SESSION['titletemp']."</h1> </div>"; $pageShowGuide->showPage(); you don't need preg_replace with nl2br Link to comment https://forums.phpfreaks.com/topic/204423-replace-newline-characters-with-html-breaks/#findComment-1070514 Share on other sites More sharing options...
Baez Posted June 10, 2010 Author Share Posted June 10, 2010 The other two preg_replaces are for BBCode not newlines. Link to comment https://forums.phpfreaks.com/topic/204423-replace-newline-characters-with-html-breaks/#findComment-1070517 Share on other sites More sharing options...
jonsjava Posted June 10, 2010 Share Posted June 10, 2010 I'm blind some times. Sorry. Link to comment https://forums.phpfreaks.com/topic/204423-replace-newline-characters-with-html-breaks/#findComment-1070519 Share on other sites More sharing options...
kenrbnsn Posted June 10, 2010 Share Posted June 10, 2010 It's not working because you're doing this <?php $descrip = nl2br($_SESSION['descriptemp']); $descrip = preg_replace("/\[b\]/", "<strong>", $_SESSION['descriptemp']); $descrip = preg_replace("/\[\/b\]/", "</strong>", $_SESSION['descriptemp']); ?> instead of <?php $descrip = nl2br($_SESSION['descriptemp']); $descrip = preg_replace("/\[b\]/", "<strong>", $descrip); $descrip = preg_replace("/\[\/b\]/", "</strong>",$descrip); ?> Notice the difference? You could also do <?php $descrip = str_replace(array('[b]','[/b]'),array('<strong>','</strong>'),$descrip); ?> instead of the two preg_replace() functions. Ken Link to comment https://forums.phpfreaks.com/topic/204423-replace-newline-characters-with-html-breaks/#findComment-1070520 Share on other sites More sharing options...
Baez Posted June 10, 2010 Author Share Posted June 10, 2010 Thanks Ken that does help but it still doesn't solve the newline problem. Link to comment https://forums.phpfreaks.com/topic/204423-replace-newline-characters-with-html-breaks/#findComment-1070532 Share on other sites More sharing options...
Alex Posted June 10, 2010 Share Posted June 10, 2010 Did you read what he said? It should. Link to comment https://forums.phpfreaks.com/topic/204423-replace-newline-characters-with-html-breaks/#findComment-1070539 Share on other sites More sharing options...
Baez Posted June 10, 2010 Author Share Posted June 10, 2010 Ah okay I read through it thoroughly but I was thinking a different way the first time. From what I saw the revised code was simply looking at the edited version of the description and replacing the BBCode with HTML. I wasn't looking at it as replacing the entire value on the second line. I'm not new to PHP by any means but sometimes we just make these ridiculous errors! Great thanks for all your help guys Link to comment https://forums.phpfreaks.com/topic/204423-replace-newline-characters-with-html-breaks/#findComment-1070545 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.