Jump to content

PHP and the textarea field


cozzy1984

Recommended Posts

Hi, just a quick question. I've got a description field on a form and have it to inserted into a text mysql field.

 

Problem is when i am displaying the data in a table with an echo statement. It doesn't output i the same way it was typed in. For instance it doesn't take new lines when i had done so we typing into the textarea. Is there a better way of doing this, possibly storing it as some other type in the database.

 

Also there is a problem in that if the user typed in a long continuous word without spaces, it makes the table i am displaying them in really wide and ruins the design. Is there a way round this also?

 

Would appreciate any ideas.

Link to comment
Share on other sites

try investing preg_replace to search the field and break and hyphenate long or mistyped words

 

<?php

$result = "OneTooManyHighlights and the start of a sentance break to prevent css distortion breakingstylestyle";
$result = preg_replace("/([^\s]{14})/","$1 ",$result);
echo $result;

?>

 

There is a forum here on Regular Expressions that might be worth exploring. The limitation on this routine is that it doesn't trap for emails or links. If that is part of your text, you will have to expand the regex. Also, don't forget the earlier nl2br() for those extra lines

Link to comment
Share on other sites

Cheers lads,

 

i now have when adding the advert:

$description = nl2br($_POST['description']);

 

and when displaying it my code is:

echo "<td colspan='2' height='60' valign='top'>".substr($description,0,150). "...</td>";

 

Which limits the character count to 150 i think and then puts ... at end.

I would prefer this to stop at word count rather than character count as it can stop in middle of a word, if anyone new code for this i would appreciate it.

 

However it still can be ruined by someone typing a contiuous long word, as it'll make the table long and upset the whole design.

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.