0

With simplexml_load_file... I've loaded an xml file. Now I want to process this file with javascript, so I created a string of it's content. Unfortunatly this string contains some returns so it can't be read. How can I get rid of these returns.

// this doesn't work because the return is not shown in the code, it just starts at a new line    
str_replace('\n','',$string);

I put in the xml as follows:

<?php
$results = simplexml_load_file('file.xml');
$resultsAsString = $results->asXML();
?>
//---
<script>    
    var xml = '<?= $resultsAsString; ?>';
</script>

And when I view the source it looks like this:

var xml = '<?xmlversion="1.0"?>
<element></element>
';

How do I remove the return after "?>" and after "/element>"

Jeroen
  • 241
  • 3
  • 21

1 Answers1

-1

Don't forget that \r is another use of a newline character. Try something like this:

<?
$resultsAsString = '<?xml version="1.0"?>
<element></element>
';
$singleLine = str_replace(array("\n", "\r"), "", $resultsAsString);
?>
<script>
    var xml = '<?=($singleLine);?>';
</script>

Edit: If you're looking to parse through the XML, may I suggest a look at this answer as well:

Cross-Browser Javascript XML Parsing

Community
  • 1
  • 1
faino
  • 3,194
  • 15
  • 17
  • Thank you! I had in mind that I did tried that.. but apparently not in the correct way. Now it works :). – Jeroen Sep 03 '13 at 12:33