2016-10-04 35 views
0

下面的代碼:的SimpleXMLElement將不保存可讀格式良好的文件

for($i = 0; $i < count(array_values($resources['titles'])); $i++){ 
       //var_dump($key); 
       $ad = $xml->addChild('ad'); 
       $ad->addChild('title', htmlentities(htmlspecialchars(substr($resources['titles'][$i], 0, 70)))); 
       $ad->addChild('text', 'Текст текст'); 
       $ad->addChild('price', htmlentities($resources['prices'][$i])); 
       //file_put_contents('test.txt',$resources['titles'][$i]."\n", FILE_APPEND); 

      } 

$xml->asXML($this->_xmlOutput); 

它保存所有的數據不錯,但XML文件格式不順利和西里爾符號(那裏有很多人的)變成&#x447(這是什麼代碼?)。另外文件保存爲ansi,而不是utf-8。所以問題是 - 如何正確地創建格式良好且可讀的(帶西里爾符號)XML文檔?

+1

'substr'對UTF-8字符串無法正常工作。 –

+0

但西里爾和格式化最新錯誤。我無法得到它。也許有辦法強制轉換爲UTF-8? – user3416803

回答

0

找到更好的解決方案。下面是循環中代碼的例子:

  $node_ad = $xml->CreateElement('ad'); 
      $node_ads->appendChild($node_ad); 

      //$node_ads->addChild('title', htmlentities(htmlspecialchars(substr($resources['titles'][$i], 0, 70)))); 
      $title = $xml->CreateElement('title', htmlentities(htmlspecialchars(substr($resources['titles'][$i], 0, 70)))); 
      $node_ad->appendChild($title); 

      $text = $xml->CreateElement('text', 'Текст текст'); 
      $node_ad->appendChild($text); 

      $images_node = $xml->CreateElement('images'); 
      $node_ad->appendChild($images_node);     

      $images = $xml->CreateElement('image', $this->_mainUrl.'/uploads/'.$resources['images'][$i]); 
      $images_node->appendChild($images); 
0

帶適當編碼標頭和標籤的前綴XML 1。在XML 第一行應該是:使用DOM文檔

<?xml version="1.0" encoding="UTF-8"?> 
+0

謝謝,但那不會改變一件事。固定到DOMDocument。 – user3416803

+0

無需引用即可正常輸出: '$ xml = html_entity_decode($ xml,ENT_NOQUOTES,'UTF-8');' – MichaEL

+0

該函數需要字符串,但我們得到了xml對象,所以它也不起作用。 – user3416803

相關問題