tags:

views:

82

answers:

2

I got a xml file that contains hierarchical data. Now I need to get some of that data into a php array. I use xsl to get the data I want formatted as a php array. But when I print it it leaves all the tabs and extra spaces and line breaks etc which I need to get rid of to turn it into a flat string (I suppose!) and then convert that string into a array.

In the xsl I output as text and have indent="no" (which does nothing). I've tried to strip \t \n \r etc but it doesn't affect the output at all.

Is there a really good php function out there that can strip out all formatting except single spaces? Or is there more going on here I don't know about or another way of doing the same thing?

+2  A: 

First off, using xsl output to form your PHP array is fairly inelegant and inefficient. I would highly suggest going with something like the domdocument class available in PHP (http://www.php.net/manual/en/class.domdocument.php). If you must stick with your current method, try using regular expressions to remove any unnecessary whitespace.

$string = preg_replace('/\s+/', '', $string);

or

$string = preg_replace('/\s\s+/', ' ', $string);

to preserve single white space.

Andrew Sledge
+1 for using explicit XML library functions instead of xsl for this. Besides the mentioned DOMDocument class, you might want to check http://de2.php.net/simplexml as an alternative.
Henrik Opel
A: 

I've created a class for opensource library that your welcome to use, and look at as an example on how to create an array from XML (and just take out the "good" parts).

USING XML

So the crux of the problem is probably keeping the data in XML as long as possible. Therefore the after the XSL translation you would have something like:

<xml>
    <data>value 
          with newline
    </data>
    <data>with      lots    of    whitespace</data>
</xml>

Then you could loop trough that data like:

$xml = simplexml_load_string($xml_string);
foreach($xml as $data)
{
    // use str_replace or a regular expression to replace the values...
    $data_array[] = str_replace(array("  ", "\n"), "", $data);
}

// $data_array is the array you want!

USING JSON

However if you can't output the XSL into XML then loop through it. Then you may want to use XSL to create a JSON string object and convert that to an array so the xsl would look like:

{
    "0" : "value 
           with newline",
    "1" : "with      lots    of    whitespace"
}

Then you could loop trough that data like:

$json_array = json_encode($json_string, TRUE); // the TRUE is to make an array
foreach($json_array as $key => $value)
{ 
    // use str_replace or a regular expression to replace the values...
    $json_array[$key] = str_replace(array("  ", "\n"), "", $value);
}

Either way you'll have to pull the values in PHP because XSLT's handling of spaces and newlines is pretty rudimentary.

null