0

I am trying to parse an KML-File in PHP and parse it using JavaScript. Now I have tried different approaches so far. My problem is, that I seem not to be able to remove every line break from the "xml". With the following method I could force all XML in one line but failed to remove spaces from "<![CDATA[...."

$dom = new DOMDocument();

$dom->preserveWhitespaces = false;
$dom->load("FILE-URL goes here");
$xpath = new DOMXPath($dom);
foreach ($xpath->query('//text()') as $domText) {
    $domText->data = trim(str_replace("\r\n","",$domText->nodeValue));
}
$dom->formatOutput = true;
$string = $dom->saveHTML();

With my next try I was able to convert everything into a string, but unable to remove any line breaks from it:

$xml = simplexml_load_file("FILE-URL goes here", 'SimpleXMLElement', LIBXML_NOCDATA);
$string = (string)$xml->asXML();
print_r(trim(preg_replace("/\r\n\t+/","",$string)));

Removing the breaks is necessary for the following JS code to be executed:

geoXml.parseKmlString('<?php print(STRING FROM ABOVE) ?>');

Unfortunately I can't download files onto the server so I am bound to something like the above. Also I can't use PHP to display the map. The KML-File itself is a normal Google-Maps-KML-File.

i alarmed alien
  • 9,412
  • 3
  • 27
  • 40
stiller_leser
  • 1,512
  • 1
  • 16
  • 39
  • 1
    Your preg_replace is looking for the exact string `\r\n\t+`. I think you want to be looking for any of those white space combinations, i.e. `/[\r\n\t+]/` – i alarmed alien Sep 26 '14 at 09:59
  • It's the simple things... That indeed was the solution. Only thing left is, that I need to find out how to put the string into '' because it contains " as well as ' :) – stiller_leser Sep 26 '14 at 10:06
  • You need to remove all instances of the string " because it contains "? You can use a `str_replace` (see my answer below) or add the string to your regex: `/([\r\n\t+]| because it contains )/` – i alarmed alien Sep 26 '14 at 10:08
  • No, sorry maybe that was not clear. I need to put the output in "" (for the JS). But it contains " as well as '. This breaks it for JS. So e.g. geoXML.parseKmlString("I am a string " that breaks) or geoXML.parseKmlString('Me I'm also breaking). But I think I will just mark the ' and " in the string. – stiller_leser Sep 26 '14 at 10:12
  • Sorry, I read the `''` as `"`! – i alarmed alien Sep 26 '14 at 10:13
  • 1
    Have you seen [add_slashes](http://php.net/manual/en/function.addslashes.php)? – i alarmed alien Sep 26 '14 at 10:15
  • Thanks, I'll keep trying! – stiller_leser Sep 26 '14 at 10:22
  • possible duplicate of [How to pass variables and data from PHP to JavaScript?](http://stackoverflow.com/questions/23740548/how-to-pass-variables-and-data-from-php-to-javascript) – ThW Sep 26 '14 at 10:55
  • This is not exactly an XML question. You're trying to escape a string for JavaScript. You can use `json_encode()` for that. – ThW Sep 26 '14 at 10:56

1 Answers1

1

Your regex in preg_replace is looking for a specific string of whitespace characters:

preg_replace("/\r\n\t+/","",$string);

I'm fairly sure you want to look for any of those characters, i.e.

preg_replace("/[\r\n\t+]/", "", $string);

You can also use str_replace with an array of the whitespace entities that you're looking for:

$ws = array("\r", "\n", "\t");
$newstr = str_replace($ws, "", $string);

Unless speed is an issue, the regex is more flexible.

i alarmed alien
  • 9,412
  • 3
  • 27
  • 40