By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,216 Members | 1,001 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,216 IT Pros & Developers. It's quick & easy.

Converting <br/> to \n and Back Again (Textarea)

cheezylu
P: 6
I'm working on a site where my client will be able to maintain some of the text on the site using input boxes and such driven by PHP and MySQL. The problem I am running into is making it easy for them to enter text into a textarea and have it automatically format the "<br/>" tags from their return strokes ("\n" in the textarea). I know how to convert the \n's to "<br/>" when a new item is added into the database using "str_replace". However, when they go to edit that field later on, I need it to convert the "<br/>"'s back to \n's (in a textarea) so it returns the original formatting they typed in and they don't have to worry about any code.
Feb 12 '07 #1
Share this Question
Share on Google+
11 Replies


stephane
P: 35
nl2br()
or, simply
str_replace()
Feb 12 '07 #2

ronverdonk
Expert 2.5K+
P: 4,258
Not that simple. <b> tags can have 3 different formats, so you have to cope with all of them. You could use a regular expression, but the following will make it more clear:

[php]$new_string=str_replace(array("<br>","<br/>","<br />"), array("\n","\n","\n"), $old_test);[/php]

Ronald :cool:
Feb 12 '07 #3

Motoma
Expert 2.5K+
P: 3,235
Not that simple. <b> tags can have 3 different formats, so you have to cope with all of them. You could use a regular expression, but the following will make it more clear:

[php]$new_string=str_replace(array("<br>","<br/>","<br />"), array("\n","\n","\n"), $old_test);[/php]

Ronald :cool:
Is str_replace case sensitive? If so, there are a lot more combinations to take into account!
Just thought I would throw that in there; regular expressions are probably the best way to go with this.
Feb 12 '07 #4

cheezylu
P: 6
[php]$new_string=str_replace(array("<br>","<br/>","<br />"), array("\n","\n","\n"), $old_test);[/php]
That is what I thought since it made sense just to use the same method to switch it back, but the new variable (ex: $new_string) appears to be blank when I try to print it to the page. Is there a secret to where this code needs to be placed?
Feb 12 '07 #5

Motoma
Expert 2.5K+
P: 3,235
The following code works for me:

[PHP]
<?php

$text = "This is some<br>crazy multi<br />lined text<br/><br><br>ohyes";
print "$text<hr>";

$new_text = str_replace(array("<br>","<br/>","<br />"), array("\n","\n","\n"), $text);
print "$new_text<hr>";

$restored_text = str_replace("\n", "<br />", $new_text);
print $restored_text;

?>
[/PHP]

Note: If you are using PHP 5 +, you can use str_ireplace for case insensitive replacement.
Feb 12 '07 #6

cheezylu
P: 6
I am using PHP 5.1.5

It still isn't working for me. Is there a special spot it needs to be placed in the code or does it matter that I'm pulling the text from a SQL database (longtext)?
Feb 12 '07 #7

Motoma
Expert 2.5K+
P: 3,235
I am using PHP 5.1.5

It still isn't working for me. Is there a special spot it needs to be placed in the code or does it matter that I'm pulling the text from a SQL database (longtext)?
Shouldn't matter.
Did the code I pasted in work?
Feb 12 '07 #8

cheezylu
P: 6
Shouldn't matter.
Did the code I pasted in work?
Yes, the code worked when I placed it as is. However, when I tried to plug in "$body" which is the field I am aiming to work with, it produced blank results. Here is my code. there are two pages needed. stories.php lists all records in the table and when you click on the "Edit" button you are directed to stories_update.php which will be the edit page for whichever item you select.

[PHP]
// stories.php

<?php include 'header.php'; ?>


<?php session_start();
header("Cache-control: private"); // IE6 fix for sessions
?>


<form method="POST" action="stories.php">
<table class=\"clear\">
<tr>
<td>Title:</td><td><input type="text" name="title"><input type="SUBMIT" value="Search!" style=\"display:inline; margin-top:0px; margin-bottom:0px;\"></td>
</tr>
</table>
</form>

<br>


<?php
session_destroy();


//Setting session variables
if( isset( $_POST[ 'title' ] ) )
{
$_SESSION['title'] = $_POST['title'];
}



//display search query
if ($title == ""){
echo("");
} else {
echo("
<table border=0 bgcolor=#ff6600><tr><td><font color=white>
You're searching for...<br>
");
}

if ($title != ""){ echo("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Title: ".$title."<br>");}

if ($title == ""){
echo("");
} else {
echo("
</font></td></tr></table>
<table cellpadding=0 cellspacing=0><tr><td>
<form method=POST action=stories.php>
<input type=hidden name=title value=\"\">
<input type=SUBMIT value=\"Clear Search Parameters\">
</form>
</td></tr></table><br>
");
}
// END of search query



// PHP Search Script
$username="***USERNAME***";
$password="***PASSWORD***";
$database="***DATABASE***";


mysql_connect("***SQL***",$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$sql = mysql_query("SELECT * FROM stories WHERE title LIKE '%$title%' ORDER BY title ASC") or die (mysql_error());

//Is displayed if there is no data to return
if(mysql_num_rows($sql) == 0){
echo("<br><br><br><b>No results found...</b><br><br>
Try widening your search a bit. :)
<br><br><br><br><br><br>");
}
// This reads the number of rows returned from the result above.


echo "

<table><tr><TD BACKGROUND=\"http://www.cheezylu.com/cheezylu/images/clear.gif\" align=\"center\">
<table cellpadding=\"1\" cellspacing=\"0\" bgcolor=\"#000000\" BORDER=0><TR><TD>
<table cellpadding=\"5\" cellspacing=\"0\" border=\"0\">

<tr bgcolor=\"#dee8f1\">
<td align=\"left\" colspan=\"4\">
<div class=\"txtTitle\" align=\"center\">Story Listing</div>
</td>
</tr>
";

$j=0;
while(list($id, $topic, $date, $author, $title, $blurb, $body, $imgTitle, $img, $imgType)=mysql_fetch_array($sql)){

if (is_int($j / 2)) {

echo "

<tr bgcolor=\"ffffff\">
<td nowrap>";

if ($img == "")
{
echo "no image";
} else
echo "<img src=\"stories_image.php?id=$id\" height=\"50\" width=\"50\">";

echo " </td>
<td>
<b>$title</b>
</td>
<td>
<form method=\"POST\" action=\"stories_update.php\">
<input type=\"hidden\" name=\"e_id\" value=\"$id\">
<input type=\"submit\" value=\"Edit\" style=\"display:inline; margin-top:0px; margin-bottom:0px;\">
</form>
</td>
<td>
<form method=\"POST\" action=\"stories_delete.php\">
<input type=\"hidden\" name=\"de_id\" value=\"$id\">
<input type=\"submit\" value=\"Delete\" style=\"display:inline; margin-top:0px; margin-bottom:0px;\">
</form>
</td>
</tr>

";

} else {

echo "

<tr bgcolor=\"#f2f2f2\">
<td nowrap>";

if ($img == "")
{
echo "no image";
} else
echo "<img src=\"stories_image.php?id=$id\" height=\"50\" width=\"50\">";

echo " </td>
<td>
<b>$title</b>
</td>
<td>
<form method=\"POST\" action=\"stories_update.php\">
<input type=\"hidden\" name=\"e_id\" value=\"$id\">
<input type=\"submit\" value=\"Edit\" style=\"display:inline; margin-top:0px; margin-bottom:0px;\">
</form>
</td>
<td>
<form method=\"POST\" action=\"stories_delete.php\">
<input type=\"hidden\" name=\"de_id\" value=\"$id\">
<input type=\"submit\" value=\"Delete\" style=\"display:inline; margin-top:0px; margin-bottom:0px;\">
</form>
</td>
</tr>

";

} //end of else

$j++;


} //end of while


echo "</table>
</td></tr></table>
</td></tr></table>";
?>


<?php include 'footer.php'; ?>
[/PHP]

[PHP]
// stories_update.php

<?php include 'header.php'; ?>


<?php session_start();
header("Cache-control: private"); // IE6 fix for sessions  
?>


<form enctype="multipart/form-data" name="frmUploadFile" action="stories_updated.php" method="post">

<table border="0"><tr><TD BACKGROUND=\"http://www.cheezylu.com/cheezylu/images/clear.gif\" align=\"center\">
<table cellpadding=\"1\" cellspacing=\"0\" bgcolor=\"#000000\" BORDER=0><TR><TD>
<table cellpadding=\"5\" cellspacing=\"0\" border=\"0\">

<tr bgcolor="#cccccc">
<td colspan="2"><b>Update Story</b></td>
</tr>

<tr bgcolor="#f2f2f2">
<td>Topic:</td>
<td>
<input type="radio" name="topicType" size="20" value="new">New Topic &nbsp;&nbsp;<input type="text" name="ud_topicNew" size="20" maxlength="50"><br/>
<input type="radio" name="topicType" size="20" value="old" checked>Existing Topic &nbsp;&nbsp;
<select name="ud_topicOld">


<?php

// Database connection variables
$dbServer = "***SQL***";
$dbDatabase = "***DATABASE***";
$dbUser = "***USERNAME***";
$dbPass = "***PASSWORD***";

$sConn = mysql_connect($dbServer, $dbUser, $dbPass)
or die("Couldn't connect to database server");

$dConn = mysql_select_db($dbDatabase, $sConn)
or die("Couldn't connect to database $dbDatabase");

$dataPull = "SELECT topic FROM stories WHERE id LIKE '$e_id'";

//Pulls what we want from the database
$data = mysql_query($dataPull) or die("Couldn't get file list");

$j=0;
while(list($topic)=mysql_fetch_array($data)){

echo "

<option value=\"$topic\">$topic *</option>";

$j++;

} //end of while


$dataTopicPull = "SELECT DISTINCT topic FROM stories";

//Pulls what we want from the database
$dataTopic = mysql_query($dataTopicPull) or die("Couldn't get file list");

$j=0;
while(list($topic)=mysql_fetch_array($dataTopic)){

echo "

<option value=\"$topic\">$topic</option>";

$j++;

} //end of while

?>

</select>

</td>
</tr>



<?php

$dataContentPull = "SELECT * FROM stories WHERE id LIKE '$e_id'";

//Pulls what we want from the database
$dataContent = mysql_query($dataContentPull) or die("Couldn't get file list");

//$restored_text = str_replace("\n", "<br />", $body);

$m=0;
while(list($id, $topic, $date, $author, $title, $blurb, $body, $imgTitle, $img, $imgType)=mysql_fetch_array($dataContent)){

echo "
<tr bgcolor=\"#ffffff\">
<td>Date:</td>
<td>
<input type=\"text\" name=\"ud_date\" size=\"15\" maxlength=\"50\" value=\"$date\"> ex: 000-00-00
</td>
</tr>

<tr bgcolor=\"#f2f2f2\">
<td>Author:</td>
<td>
<input type=\"text\" name=\"ud_author\" size=\"30\" maxlength=\"50\" value=\"$author\">
</td>
</tr>

<tr bgcolor=\"#ffffff\">
<td>Story Title:</td>
<td>
<input type=\"text\" name=\"ud_title\" size=\"30\" maxlength=\"50\" value=\"$title\">
</td>
</tr>

<tr bgcolor=\"#f2f2f2\">
<td>Blurb:</td>
<td>
<input type=\"text\" name=\"ud_blurb\" size=\"30\" maxlength=\"100\" value=\"$blurb\">
</td>
</tr>

<tr bgcolor=\"#ffffff\">
<td valign=\"top\">Story:</td>
<td>
<textarea name=\"ud_body\" cols=\"30\" rows=\"10\">$body</textarea>
</td>
</tr>

<tr bgcolor=\"#f2f2f2\">
<td>File Description:</td>
<td>
<input type=\"text\" name=\"ud_imgTitle\" size=\"30\" maxlength=\"50\" value=\"$imgTitle\">
</td>
</tr>

<tr bgcolor=\"#ffffff\">
<td valign=\"top\">Update Image:</td>
<td>
<input type=\"radio\" name=\"imgUpdate\" value=\"clear\">Clear Image<br/>
<input type=\"radio\" name=\"imgUpdate\" value=\"n\" checked>No<br/>
<input type=\"radio\" name=\"imgUpdate\" value=\"y\">Yes &nbsp;&nbsp;
<input type=\"file\" name=\"fileUpload\" size=\"20\"><br/>
</td>
</tr>

<tr bgcolor=\"#f2f2f2\">
<td colspan=\"2\" align=\"center\">
<input type=\"hidden\" name=\"ud_id\" value=\"$id\">
<input type=\"submit\" value=\"Update Story\" name=\"cmdSubmit\">
</td>
</tr>
";

$m++;

} //end of while

?>



</table>
</td></tr></table>
</td></tr></table>

</form>


<?php include 'footer.php'; ?>
[/PHP]
Feb 12 '07 #9

Motoma
Expert 2.5K+
P: 3,235
Try doing a little debugging!
Craft in a couple of echo statements, before and after the str_replace();
Are you getting to the code after it, or is the script halting?
What is the content of $body before and after?
$restored_string?
Do you have errors on? Do you get any messages when you do?
Feb 12 '07 #10

cheezylu
P: 6
I am a complete idiot. I was putting the code outside my while loop! Eeesh!

Thank you very much for all of your patience. I had been working on all of this for two whole days with very little sleep. I guess I have learned my lesson that sleep, while time consuming, is quite useful. Thanks again!
Feb 13 '07 #11

Motoma
Expert 2.5K+
P: 3,235
I am a complete idiot. I was putting the code outside my while loop! Eeesh!

Thank you very much for all of your patience. I had been working on all of this for two whole days with very little sleep. I guess I have learned my lesson that sleep, while time consuming, is quite useful. Thanks again!
Glad you got it all figured out, and welcome to The Scripts!
Feb 13 '07 #12

Post your reply

Sign in to post your reply or Sign up for a free account.