Reputation: 1572
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.
Upvotes: 0
Views: 347
Reputation: 9520
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.
Upvotes: 1