Scripting: PHP Text

started by Vladimir on Apr 18, 2004 — RSS Feed

Vladimir Vladimir
Posts: 53

Hi,

I haven't tried yet but let's say I've put poems into a database. I use PHP to extract each poem to display or edit on a webpage. But poems are not just one-word text and they contain line breaks. Does the database store this information as well? If not, how can I make it do so? Thanks.

Joe Gillespie Joe Gillespie
Posts: 528

Line breaks are an intregral part of 'plain text', you just have to make sure that you are using the correct flavour.

There are three types of line break:-

Mac - CR Ascii 13
DOS - CRLF  Ascii 13 Ascii 10
UNIX - LF  Ascii 10

If you make sure you are uing the UNIX LF, you will get your line breaks.

Vladimir Vladimir
Posts: 53

Is there a character limit on a database entry, something like 255? Because a poem won't fit then....

Vladimir Vladimir
Posts: 53

I looked at a book and it said to add <p> tags in the actual database field

Mountie Mountie
Posts: 11

Vladimir said:
Is there a character limit on a database entry, something like 255? Because a poem won't fit then....


It depends on the way your table was set up.  Most DB implementations allow you to specify the type and size of data to be stored in a particular field.  For example, MS Access allows you to do text fields up to 255 characters, as well as Memo fields with up to several thousand characters.  Check your particular DB's documentation for specifics.

Vladimir Vladimir
Posts: 53

I use MySQL, and I created a TEXT field to include the poems. However, in the inserting page, even though the line breaks show in the textbox,  the line breaks are gone when I extract it out of the database.

My only solution right now is to add <br /> s right in the fields...but that is not practical.  

Jdenny Jdenny
Posts: 65

I would get it to automatically convert CR/LF to <br /> when it saves it into the DB.

Terakris Terakris
Posts: 23

I would have got it to convert CR/LF from out of the database instead. This way, if you have an online admin screen where someone is inputing the data, they dont need to know to put in <BR> when they put a CR/LF in or even get worried when they start to see them in the admin screen.

Depends on your setup really i guess.

Vladimir Vladimir
Posts: 53

Thanks for your suggestions. But what is CR/LF and how do you convert it to <br />? I think doing that while displaying the data is a better idea then saving the <br />s in the field.

Terakris Terakris
Posts: 23

Carrage Return/Line Feed (or the equivilent of you pressing the return key).

Not sure how to convert it in PHP as its not my best area... can do it in ASP if that helps!  

Terakris Terakris
Posts: 23

Found this... hope it helps:

http://www.zend.com/manual/function.str-replace.php

Jdenny Jdenny
Posts: 65

I thought you said the database was not storing the CR/LF ? that's why I said convert before storage.

If the database saves it all correctly with new-lines intact the simply wrap the oputput on the page in <pre> tags!

Vladimir Vladimir
Posts: 53

Ok thanks guys. I solved it.  

Scott Thompson Scott Thompson
Posts: 9

Pretty easy... i hope you solved it properly without XSS or SQL injection attacks?

It SHOULD be something like:

$text = htmlentities(mysql_real_escape_string($_POST['article']), ENT_QUOTES);

and then you should insert $text in to the database. when you display it, to show the line spaces it should be like:

print nl2br($article);

Kind regards,
Scott

If you enjoy programming, then checkout my coder profile http://www.coderprofile.com/coder/VBAssassin

You must login to reply