views:

10

answers:

2

I'm writing note software in PHP (to store notes) and most often I include code within, when I fetch the note from the database it collapses all whitespace I assume, so any code blocks look ugly. (I nl2br() it, I mean horizontal space)

What would be the most efficient way to deal with this? I think the database entry keeps the spaces, so would replacing all spaces with   be the only solution PHP-display-side? (ugly for long long entries), what are your thoughts on how I can accomplish this taking in mind the code may be 1-16M characters long?

+1  A: 

It shouldn't be collapsing all whitespace. Try outputting it inside <pre> tags to see that white space.

Josh K
Would this be the only efficient way? I don't like monospace fonts unless it's only for code, otherwise it's ugly.
John
@John: The most efficient way would be to use something like [Markdown](http://michelf.com/projects/php-markdown/) and convert the source to html and store the html code along with the source in the database.
Josh K
@Josh, I guess, but then again space into ` ` will require many less processing and physical space taken by tags. EDIT: You know what, Markdown would be fun for my notes. Thank you alot for the idea.
John
@John: I find Markdown to be the easiest "markup" language to use, so I implemented it just about everywhere I need to mark stuff up.
Josh K
@Josh: There's a nice plethora of PHP markdown libraries, Many thanks. I'll accept.
John
A: 

What code are you storing the Database? HTML? PHP?! This will determine the best solution to your problem.

Different column types will or won't preserve characters like new lines, carriage returns or tabs. I use Text, using a UTF-8 collation.

At a very basic level look at nl2br() - http://php.net/manual/en/function.nl2br.php

jakenoble
I mentioned I'm using `nl2br`. :PI won't be using tabs, only spaces (worst case I can translate tabs into spaces), just needed help with horizontal formatting. I'm storing plaintext into MySql through PHP to answer your question.
John
Apologies. Looks like `<pre>` might give you what your after, by seeing what it is your saving. If it is removing the spaces ` ` is your solution
jakenoble