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

Posted on 2006-04-11
Last Modified: 2008-03-06

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?

Question by:MaritimeSource
    LVL 25

    Accepted Solution

    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.

    Author Comment

    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
    LVL 12

    Assisted Solution

    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!

    LVL 25

    Expert Comment

    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.


    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Join & Write a Comment

    Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
    Why do we like using grid based layouts in website design? Let's look at the live examples of websites and compare them to grid based WordPress themes.
    This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now