processing line breaks: html text area to database to xml: ^M


I have an adhoc cms system, where the user types in content into a text area. Then I save it to a mysql db. Then I have a script which pulls from the DB and inserts into an xml file using php DOM XML functions (php 4). The problem is, in the xml file the line breaks are coming out like so:


I should convert these when I pull it from the database, but before I write it to the xml. THe question is, what character code is this and what character code do I replace it with?

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

When you hit enter a Carriage Return/Line Feed pair are inserted into most programs.

This is ASCII chars 13 and 10 respectively.  Depends upon where you will be displaying the string and if you want to keep the line breaks. If it will be displayed as HTML then you could change it to <BR>

If you don't want to keep line break, strip them out.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MaritimeSourceAuthor Commented:
I want to keep the line break, but I don't want it to show up as ^M in the xml output file. I think this is happening because the web app is used on a windows machine, but the xml is created on a linux server. So I think I should strip the ASCII char 10 and leave the 13 in there? I want to kee the line break, but don't want to convert it to <br> for other reasons. I think the 10 is the one windows introduces.

Why would microsoft ever do such a thing anyway??? DOH
yea, microsoft is evil, congrats on using a linux server ;)

>>I think the 10 is the one windows introduces.  
As mentioned above windows inserts "\n\r" every time you hit the enter key.  Also the textarea should have wrap=virtual or wrap=soft so that the word wrap line feeds aren't added to the db.  I believe the default is soft but if wrap=hard is specified then those word wraped line terminations will appear in the db.

...anyway, if you wanna keep them, first you'll need to figure how to insert on linux, i.e. what character linux uses, I believe it's just a single '\n'

...then, one idea would be to replace the "\n\r" with an identifiable string which will be replaced again on the client.  A string I use a lot for this kinda thing is"-----"

It's a huge pain in the butt!  It's not entirely Microsofts fault either.  I mean damn, can we just use ONE character set, is it that hard?!?  I've experimented with unicode and honestly, don't know if I'm dumb, but that didn't really work either, you've still gotta translate from whatever damn character set the data originated, huge can o worms.

...I wish you luck!

You are correct.  Unix only uses one of the chars, but Microsoft uses two chars.
Well Carriage return is to go back to the start (left side) horizontally and linefeed to go to the next line.  I guess this carried over from something in computer history.

I would try getting rid of the 10 and see if it works. I assume you are displaying this on a Unix/linux machine and that is why it is a problem.

The <BR> in HTML is platform independent, but I understand why you don't want to fool with it.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Development

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.