Solved

Inserting carriage returns in xml text

Posted on 2011-03-16
8
572 Views
Last Modified: 2013-11-18
I'm using PHP to query an MySql database and produce an xml file for another site.
I'm concating 3 of the database fields into one xml tag but want to add a carriage return between them. What should I be using?

$xmltext = $field1. "carriage return here".$field2. "carriage return here".$field3;

$xmltext = htmlspecialchars($xmltext);
$xmltext = addslashes($xmltext);

$_xml .="<Description>".urlencode($xmltext)."</Description>\r";
0
Comment
Question by:fionafenton
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 8

Expert Comment

by:Rik-Legger
ID: 35147144
You want to know how to add an seperator with mysql?
http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_concat-ws
mysql> SELECT CONCAT_WS(',','First name','Second name','Last Name');
        -> 'First name,Second name,Last Name'

Open in new window


Or do you need some advice on what sign to use as seperator?
In that case i would just use something then never will be used in the field texts.
My suggestion is just a semicolon (;) but i'm not aware of your data.
0
 
LVL 1

Author Comment

by:fionafenton
ID: 35147219
No, I want to know what to use for a carriage return so that it displays correctly when inserted into an xml tag.
I've tried <br>, &#xA;,&#xD; and &#13;&#10;  but they display exactly as written when displayed on the final web page.
i.e.
text from field 1.&#13;&#10; text from field 2.&#13;&#10; text from field3.

Rather than
text from field 1.
text from field 2.
text from field 3.
0
 
LVL 8

Expert Comment

by:Rik-Legger
ID: 35147250
Than stop using this:
$xmltext = htmlspecialchars($xmltext);
$xmltext = addslashes($xmltext);

Open in new window


And start using htmlentities() and html_entity_decode
http://www.php.net/manual/en/function.htmlentities.php
http://php.net/manual/en/function.html-entity-decode.php
0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
LVL 1

Author Comment

by:fionafenton
ID: 35147628
I have no control over what happens to the data at the other end, so html-entity-decode is irrelevant.
If I use <br> as the carriage turn it shouldn't make any difference whether I use htmlspecialchars or htemlentities. However, when I use <br> it is displayed as that in the browser.
I'm sure it's to do with the way the other website (which I'm feeding into) handles the data. I think I need a non html way of inserting a carriage return. I have also tried /n and /r but these don't work either.
0
 
LVL 8

Expert Comment

by:Rik-Legger
ID: 35147651
If you don't have control over the other website and the other website does not 'decode' your encoded data than this is impossible.
0
 
LVL 1

Author Comment

by:fionafenton
ID: 35153349
I found the solution myself through trial and error.
I needed to use %0a and not urlencode it.

My code is now like this

$xmltext = $field1. "carriage return here".$field2. "carriage return here".$field3;

$xmltext = htmlspecialchars($xmltext);
$xmltext = addslashes($xmltext);

$_xml .="<Description>".urlencode(addslashes(htmlspecialchars($field1)))."%0A".urlencode(addslashes(htmlspecialchars($field2)))."%0A".urlencode(addslashes(htmlspecialchars($field3)))."%0A"."</Description>\r";
0
 
LVL 1

Accepted Solution

by:
fionafenton earned 0 total points
ID: 35153361
Sorry - my code is now like this (remove the first 3 lines of code above)

$_xml .="<Description>".urlencode(addslashes(htmlspecialchars($field1)))."%0A".urlencode(addslashes(htmlspecialchars($field2)))."%0A".urlencode(addslashes(htmlspecialchars($field3)))."%0A"."</Description>\r";
0
 
LVL 1

Author Closing Comment

by:fionafenton
ID: 35178868
Found solution myself
0

Featured Post

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

628 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