gpt4 book ai didi

PHP iconv 错误

转载 作者:可可西里 更新时间:2023-11-01 13:24:28 25 4
gpt4 key购买 nike

当我使用 ASP Classic 脚本生成 XML 文件并在 PHP 页面中导入 XML 文件时,导入过程工作正常。

但是,当我通过 PHP 脚本(而不是 ASP Classic)生成相同的 XML 并在相同的导入过程中使用它时,它就不起作用了。

$xml = iconv("UTF-16", "UTF-8", $xml);

我在导入过程中注意到:

  • 在我的代码中的 $xml = iconv("UTF-16", "UTF-8", $xml); 行之前,XML 文件的格式正确。
  • 但在 $xml = iconv("UTF-16", "UTF-8", $xml); 行之后,XML 文件已损坏。

当我注释掉这行代码并使用 PHP XML 文件时,它工作正常。

最佳答案

资源:PHP official site- SimpleXMLElement documentation

如果您声称此行有错误:

$xml = iconv("UTF-16", "UTF-8", $xml);

然后将其更改为这样,因为 $xml 可能不是“UTF-16”:

$xml = iconv(mb_detect_encoding($xml), "UTF-8", $xml);

保存 XML 文件:

//saving generated xml file
$xml_student_info->asXML('file path and name');

要导入 xml 文件:

$url = "http://www.domain.com/users/file.xml";
$xml = simplexml_load_string(file_get_contents($url));

如果你有一个数组如下:

$test_array = array (
'bla' => 'blub',
'foo' => 'bar',
'another_array' => array (
'stack' => 'overflow',
),
);

并且您希望将其转换为以下 XML:

<?xml version="1.0"?>
<main_node>
<bla>blub</bla>
<foo>bar</foo>
<another_array>
<stack>overflow</stack>
</another_array>
</main_node>

然后是 PHP 代码:

<?php

//make the array
$test = array (
'bla' => 'blub',
'foo' => 'bar',
'another_array' => array (
'stack' => 'overflow',
),
);

//make an XML object
$xml_test = new SimpleXMLElement("<?xml version=\"1.0\"?><main_node></main_node>");

// function call to convert array to xml
array_to_xml($test,$xml_test);

//here's the function definition (array_to_xml)
function array_to_xml($test, &$xml_test) {
foreach($test as $key => $value) {
if(is_array($value)) {
if(!is_numeric($key)){
$subnode = $xml_test->addChild("$key");
array_to_xml($value, $subnode);
}
else{
$subnode = $xml_test->addChild("item$key");
array_to_xml($value, $subnode);
}
}
else {
$xml_test->addChild("$key","$value");
}
}
}

/we finally print it out
print $xml_test->asXML();

?>

关于PHP iconv 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19194937/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com