473,386 Members | 1,610 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

cheezylu
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
11 4447
nl2br()
or, simply
str_replace()
Feb 12 '07 #2
ronverdonk
4,258 Expert 4TB
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
3,237 Expert 2GB
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
[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
3,237 Expert 2GB
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
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
3,237 Expert 2GB
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
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
3,237 Expert 2GB
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
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
3,237 Expert 2GB
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

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

Similar topics

4
by: Christopher Finke | last post by:
I have a site wherein the clients can update the individual pages by editing the text of that page in a <textarea>. Then, when the page is displayed, all of the newlines (\n) are converted to HTML...
4
by: fis | last post by:
Hi all, I've problem because there are needed break lines in my texts on the web site but i can't do it :( My pipeline looks like: XMS -> I18N -> XSLT -> HTML I have lot of texts in my...
6
by: Lasse | last post by:
I have done this simple function, it seems to work as intended, to solve a problem i have had for a while. I couldnt find any sample around that was working for me. I would like to test it with...
7
by: noor.rahman | last post by:
I have an XML file that stores data from an HTML form. I use XSL to display the data in HTML format. The data may have newline characters. However, XSL is not displaying the newlines properly in...
7
by: Rocky Moore | last post by:
I have a web site called HintsAndTips.com. On this site people post tips using a very simply webform with a multi line TextBox for inputing the tip text. This text is encode to HTML so that no...
1
by: susikaufmann2003 | last post by:
Hello, i have a html text, where it is possible (using ajax) to edit the text by clicking on it. The text is then shown in a textarea. My problem is, that the newlines are not displayed in the...
2
by: xhe | last post by:
I met a very headache problem in javascript, I think this might be difference between IE and NS / Safari. I have a text area <form> <textarea name='tex1' onkeyup='displayit();'></textarea>...
3
by: ajay2552 | last post by:
Hi, I have a query. All html tags start with < and end with >. Suppose i want to display either '<' or '>' or say some text like '<Company>' in html how do i do it? One method is to use &lt,...
8
luke14free
by: luke14free | last post by:
Hello, I'm creating a form to let user leave comments, I'm having a problem, because I need to convert all the \n to <br/>. After several tries I came out with this simple function <script...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.