Jump to content

Replace Newline Characters with HTML Breaks


Baez

Recommended Posts

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
Share on other sites

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
Share on other sites

$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
Share on other sites

$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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.