block of text

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
turgs
Forum Newbie
Posts: 6
Joined: Fri Apr 07, 2006 1:56 pm

block of text

Post by turgs »

what data type should I use for mySQL for Text Blocks like this area I'm typing in right now...???

on which, the format of the text will be preserve like carriage returns...

is varchar just alright?
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

For your first question the answer is: No, varchar will be fine for strings no longer than 255 chars!
I guess you want to be able to store strings longer than 255 chars... for that, use TEXT (up to 65535 chars).
If you need more than 65535 chars check out this: http://htmlite.com/mysql003.php

And for your second question... I really can't understand your question, can you please try to explain it better? (or perhaps I'm the only one who can't understand it? :?)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

All of the TEXT based types in MySQL preserve the formatting, technically. To display them as such normally involves nl2br() and replacing all leading whitespace with   pairs.
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

Oh... now I get you. feyd is right.
Let's say you read some data from the db and put in: $data
Use this code:

Code: Select all

<?php

	$data = htmlspecialchars($data, ENT_QUOTES);
	$data = str_replace(' ', '&nbsp;', $data);
	$data = nl2br($data);

	echo $data;
?>
Make sure to call the functions in this specific order, it is extremely important or otherwise the date won't be shown correctly!
Post Reply