• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 342
  • Last Modified:

Text formatting

Hi, I have created a web page that allows the selling of antique furniture. The website in made using php as the front end and MySql as the database.
I have created an Editing suite for the client to add/amend and delete items on the website. The issue we are having is in the text areas, we have to use HTML break tags as line breaks. This is very frustrating for my client and has asked me to find an alternative solution to using the HTML tags?
My question is, is there a way of creating a class/script that will add the HTML tags automatically and format the text in the textboxes?

screenshot1 is a screenshot of the textboxes in the editing suite.
screenshot2 is the screenshot of the website. screenshot1 screenshot2
0
irwells
Asked:
irwells
4 Solutions
 
Rik-LeggerCommented:
You can use the function nl2br() for this.

// $aRow['field'] contains the text from the database
echo nl2br($aRow['field']);

Open in new window

0
 
Ray PaseurCommented:
You might be able to use nl2br()
0
 
Ray PaseurCommented:
But if you want a more comprehensive solution (albeit a little more work) learn about TinyMCE.  It is the textarea processor used by WordPress.  Allows lots of easy client-side text formatting, etc.  The only problem I have had with TinyMCE occurred when clients copied from Word documents.  A bit of client education solved that issue.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Tom BeckCommented:
It seems to me the problem is capturing the new lines in the textarea. I don't understand how nl2br() can help here. I can think of only two possibilities aside from third party add-ons.

1. Capture the carriage returns using javascript and convert them to <br /> before submitting. This is problematic because clinking Enter can also submit the for if the submit button has focus.

2. Use a single line textbox and provide a button below it that says "New Line". The client can add new lines as desired. You can add new textboxes using the javascript DOM and assemble the inputs client side, appending <br />s between each textbox value, before submitting the form.
0
 
Mohamed AbowardaSoftware EngineerCommented:
0
 
Ray PaseurCommented:
This script will illustrate a bit of the principle about nl2br()
http://www.laprbass.com/RAY_nl2br.php

Here is the way the pros might do it... on the input side:
1. Receive the input string from the textarea of the form
2. Use mysql_real_escape_string() to sanitize it for storage in the data base
3. Store it in the data base.

On the output side:
1. SELECT the text from the data base
2. Use strip_tags() to remove any unwanted HTML tags
3. Use nl2br() to inject the break tags
4. Echo the string to the browser output stream.

You might add additional filtering and sanity checks, but that sequence should get you what you want, which is new lines in the displayed data.

HTH, ~Ray
<?php // RAY_nl2br.php
error_reporting(E_ALL);


// IF THE FORM WAS POSTED
if (!empty($_POST["t"]))
{
    // MAKE NEW-LINE CHARACTERS INTO BREAK TAGS
    $t = nl2br($_POST["t"]);

    // SHOW HOW THE nl2br() FUNCTION CHANGES THE TEXT
    echo "<pre>";
    echo "THE INPUT STRING CONTAINS: {$_POST["t"]}";
    echo PHP_EOL;
    echo "THE TRANSFORMED STRING CONTAINS: ";
    echo htmlentities($t);
}


// CREATE THE FORM
$f = <<<FORM
<form method="post">
<textarea name="t"></textarea>
<input type="submit" />
</form>
FORM;

echo $f;

Open in new window

0
 
irwellsAuthor Commented:
wow, thanks guys for a quick responce.

I will play around with my code tomorrow and see if it work.

Will keep you posted.

Many Thanks
Ian
0
 
irwellsAuthor Commented:
hi guys,
I have tried both str_replace and nl2br() on $POST and $row on the text areas and both work beautifully. I have decided to use nl2br() on $row.

$description=nl2br($row["description"]);
$dimension=nl2br($row["dimension"]);

this is because using nl2br() or str_replace on $POST adds the <br /> marks to the text in the database and shows the <br /> marks when i update and refreah the Editing page.

The only issue i have is that my tooltip does not work using nl2br() or str_replace. For some stange reason the tooltip will only diplay if the text is formatted in one single line:

Height 46.5cm<br />Diameter 25cm

and not formatted in multiable lines:

Height 46.5cm
Diameter 25cm<br />

can anyone give advive on this???

Ian


0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now