从XML元素中删除开始和结束空格

Ell*_*lla 5 php xml string simplexml

如何在XML字段之前和之后删除所有间距字符?

<data version="2.0">

  <field> 

     1 

  </field>        

  <field something=" some attribute here... "> 

     2  

  </field>

</data>
Run Code Online (Sandbox Code Playgroud)

请注意,在1和2之间的间距和'some attribute here ...',我想用PHP删除它.

if(($xml = simplexml_load_file($file)) === false) die();

print_r($xml);
Run Code Online (Sandbox Code Playgroud)

此外数据似乎不是字符串,我需要在每个变量之前追加(字符串).为什么?

Cod*_*ter 1

由于simplexml_load_file()将数据读取到数组中,因此您可以执行以下操作:

function TrimArray($input){

    if (!is_array($input))
        return trim($input);

    return array_map('TrimArray', $input);
}
Run Code Online (Sandbox Code Playgroud)