Forum Moderators: open

Message Too Old, No Replies

How do I see user's raw input (line break characters included)?

view or display hidden or raw or special or escaped characters, line break

         

elbowlobstercowstand

6:36 pm on Dec 19, 2007 (gmt 0)

10+ Year Member



Bottom Line:
I'm trying to see user's raw input from a textarea field; specifically WHICH line break characters are used.

Background:
I have a LAMP (linux, apache, mysql, php) server. People connect on PCs and Macs (and other things I don't know about), via a myriad of browsers, enter paragraphs into a textarea on my form, and hit submit. I do a mysql_real_escape_string() on the data, and store it into mysql (charset = utf8_unicode_ci).

Questions:
1) I've heard line break characters can vary from \n to \r to \r\n depending on the system. But what system are we talking about? The client system or the server system?
2) I tried looking at the raw input in phpmyadmin. No bueno. Then I thought MySQL Monitor would be the daddy to do this. Alas, it too displays the physically visible new line as opposed to the character itself. I searched for a display option (similar to \G) to tack on to the end of a MySQL Monitor command. Couldn't find one.
a) Is there a MySQL Monitor command that shows raw input?
b) Is there ANY way to show raw input?
3) Am I over analyzing things?

PS: If you said yes to 3), note that I read the entire comment thread regarding the php function nl2br at php dot net (www.php.net). It got my head spinning on just which method to use in displaying user input (back to other users) as they typed it in, line breaks and all. So I thought a good place to start was to first understand what type of special/hidden/escaped characters come with the input.

Thanks for your help!

PPS: I suppose if the line break characters are dependent only on the server side system (my system), then I know it will always be \n... right? Thus, question 2 won't really matter to me anymore. But maybe this will help someone else too.

coopster

4:58 am on Dec 20, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



1) I've heard line break characters can vary from \n to \r to \r\n depending on the system. But what system are we talking about? The client system or the server system?

The operating system from which the characters were typed, so in your case it is likely the client system (user-submitted data).

a) Is there a MySQL Monitor command that shows raw input?
b) Is there ANY way to show raw input?
3) Am I over analyzing things?

You can read the information in binary format with a good text editor/analyzer that allows you to view the character codes in hexadecimal format. I often use textpad to do so, but there are many other viewers available.

It all depends on what you are attempting to do here and for what purpose. In most cases, you simply want to reflect the line breaks the same way the user has keyed them. If that is the case, you can convert either the \n or the \r or a combination of \r\n to a newline that renders properly in a browser -- that is the purpose of the nl2br() function in PHP. Most RTE (Rich Text Editors) will handle this for you as well. In a lot of cases, they will be removed and the text blocks might be enclosed in separate <p> containers. You can use your own markup (CSS) to style the output as desired.

elbowlobstercowstand

4:34 pm on Dec 20, 2007 (gmt 0)

10+ Year Member



Thanks coopster! Question 1) was the one I was mainly interested in understanding. Will still keep my eyes peeled for a MySQL Monitor way to see the special characters.

I've used the nl2br function to display user inputed text before, and sometimes there are quadruple line breaks (as the user is copying/pasting info likely from ms word). That formatting drives me mad. I'll research the comments on the nl2br php.net page; I'm pretty sure their are some user inputed functions that handle all three new line characters more elegantly than nl2br.

coopster

4:50 pm on Dec 20, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



It depends on how the user is keying the information, as you suspect. If they intended to have 4 newlines then you likely will want to leave it that way. If you are merely looking for ways to clean up MS Word cut/paste, you may want to have a look at an RTE (Rich Text Editor) like TinyMCE (or a host of others) that have already been written and provide that option.

elbowlobstercowstand

6:49 pm on Dec 20, 2007 (gmt 0)

10+ Year Member



I am mainly looking at a way to clean copy/paste issues. So I'll check out TinyMCE. Thanks for the advice.

Soon I will be offering my users a way to, instead of copying/pasting, upload their info. I want to keep that as close to the original as possible (usually from .doc files), but still shove it into a db.

4) So a follow up question: which PHP function would be best at keeping line breaks in place:

fgets() ¦¦ fread() ¦¦ file_get_contents()

I feel the answer is file_get_contents(), however, not sure that his preserves line breaks or not. I would guess it does. I should test it.

coopster

7:41 pm on Dec 20, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



The functions you describe are functions that read data from a file. Where is this file? I thought the data was coming from a <textarea> element?

It sounds like you are using a standard <textarea> element to accept plain text and push that into a text file or something on your server. If your goal is to allow your end user to copy/paste from a word processing document and maintain as much as the original document formatting as possible, I would encourage you to have a closer look at rich text editors. They use JavaScript on the client side (in the user's web browser) to change a standard textarea element into a rich text editing tool. The text is formatted with HTML which can then be rendered in a web page as intended.

If you are merely grabbing plain text entered into your <textarea> and storing it as is in a file on your filesystem, you can use any of the file reading functions described to pull the file into a variable so you can format it as desired. "Formatting it as desired" is the part you want to have a closer look at -- perhaps a regular expression to match and replace any variation of newlines is what you require?

elbowlobstercowstand

3:13 am on Jan 4, 2008 (gmt 0)

10+ Year Member



Sorry for the delay. I apparently went on an extended vacation and forgot that I had code to write :o) Happy Christmas and Merry New Year people!

Hopefully this will clear up the confusion I created:

How I Handle Text Entry Now:
On my site, registrants can currently enter text into a textarea html element. They hit submit, and that info is entered into a mySQL db. The only "cleaning" function i use is mysql_real_escape_string(). Problem is a) users hate copy/pasting text and b) when they do, it usually looks really bad.

How I Want to Handle Text Entry (in the future):
I want registrants to be able to upload a file (generally a .doc or .txt or .rtf) ... OR enter the text directly. I would handle the "enter text directly" scenario like I do now. In the upload scenario, I want to read the file from the client's machine, upload the text to a textarea box (with line breaks in place), have user confirm it and hit submit, and finally, throw the text into mySQL db using mysql_real_escape_string

I plan on using one of the nl2br variations on php.net (possibly one with a reg exp). I want to be able to handle line breaks from any system.

I looked into a rich text editor, but I want to try to stay away from that... keep things real simple for the user. Like... you want a heading? Then make it ALL CAPS. Stuff like that. Plus, with uploading a file, I'm not sure I could preserve formatting like bold, italics anyway.

This thread has really helped me figure out how to go about things. But maybe there is even a better way... any suggestions are totally invited.