How to deal with newlines inserted when pasting into a textarea on the Mac
August 24, 2006 5:35 AM Subscribe
Pasting text into an HTML textarea on a Mac inserts newlines where the lines wrap, but this doesn't happen on Windows. It's messing up my very basic content management.
posted by lockedroomguy to Computers & Internet (9 answers total)
Our website has a php data entry feature to allow the loading of articles into a database. We are posting a lot of existing data, so we're cut-and-pasting it into a text area to be loaded into a MySQL database.
I'm searching for newlines (/n) in the text in the textarea, and replacing with paragraph tags so the articles display nicely in HTML. The newlines already exist in the source file. Users just select an article to enter, then copy it, and paste it into the textarea on the data entry page.
(NOTE: We are not hitting return inside the textarea, so this is not simply the common problem of Windows and Macs using different control characters for a Return. We are only pasting into the texarea.)
The process works if you use a Windows machine to do the copy-and-pasting. In that case the newlines are replaced with paragraph tags. But on a Mac, newlines are added at every point the text wraps in the textarea box.
For example, imagine that the following is text that you have just entered into a textarea in a browser on your (Windows) screen. Most of the wrapping you see would be because of the width of the textarea box. But after the "entering.", there is actually a return (/n).
Here is the first
paragraph of some
text that I am
This is the second
When I replace /n with a paragraph tag, the resulting field in my database contains just the text, with a paragraph tag between the word "entering." and before the word "This". The other line wraps are only a function of the width of the textarea box and have nothing to do with the source data, so no paragraph tags are inserted there. This is what I want.
But if you do the data entry on a Mac, using Safari, the result is a newline (that is, a /n) inserted everywhere the text wraps in the text area. The result of this is that my search and replace for newlines inserts a paragraph at the end of every line in the textarea, instead of at the end of every paragraph.
This is not because I'm opening the source data file on a Mac - I say this because in every case the newlines are inserted exactly where the text wraps in the textarea, so it's something to do with the way the browser works on a Mac, or something to do with Safari, or... something.
I can get around this by changing the data - by inserting the tags I want directly into the textarea and skipping the replace, or by inserting an additional newline in the source file and searching and replacing any instance of 2 adjacent newlines with a paragraph tag.
Is there a way to get around this without changing my data?