gpt4 book ai didi

php - 如何使用 php 以正确的编码重写此 xml 文件?

转载 作者:行者123 更新时间:2023-12-05 04:43:21 25 4
gpt4 key购买 nike

我想用 php 重写一个没有编码字符的 xml 文件。

我的 xml 文件如下所示:

<order>
<orderId>9132</orderId>
<statusId>3</statusId>
<adminUrl>www.floros24.gr/wp-admin/post.php?post=9132&amp;action=edit</adminUrl>
<status>&#x39F;&#x3BB;&#x3BF;&#x3BA;&#x3BB;&#x3B7;&#x3C1;&#x3C9;&#x3BC;&#x3AD;&#x3BD;&#x3B7;</status>
<dateCreated>2021-06-26T22:24:56+03:00</dateCreated>
<dateCompleted>2021-06-28T10:56:05+03:00</dateCompleted>
<dateModified/>
<datePaid>2021-06-28T10:56:05+03:00</datePaid>
<paymentMethodId>1</paymentMethodId>
<paymentMethod>&#x391;&#x3BD;&#x3C4;&#x3B9;&#x3BA;&#x3B1;&#x3C4;&#x3B1;&#x3B2;&#x3BF;&#x3BB;&#x3AE;</paymentMethod>
<userId>-1</userId>
<notes/>
<total>53.17</total>
<vatAmount>12.76</vatAmount>
<amount>65.93</amount>
<billing>
<firstName>&#x395;&#x3C5;&#x3B1;&#x3B3;&#x3B3;&#x3B5;&#x3BB;&#x3AF;&#x3B1;</firstName>
<lastName>&#x3A4;&#x3C1;&#x3B9;&#x3B1;&#x3BD;&#x3C4;&#x3B1;&#x3C6;&#x3CD;&#x3BB;&#x3BB;&#x3BF;&#x3C5;</lastName>
<address>25&#x3B7;&#x3C2; &#x39C;&#x3B1;&#x3C1;&#x3C4;&#x3AF;&#x3BF;&#x3C5; 23</address>
<city>&#x3A3;&#x3AF;&#x3BD;&#x3B4;&#x3BF;&#x3C2;</city>
<postcode>57400</postcode>
<state>B</state>
<country>GR</country>
<phone>2310797958</phone>
<cellphone/>
<email>etrian@gmail.com</email>
<isInvoice>0</isInvoice>
<vat/>
<taxOffice/>
<activity/>
</billing>

如何解决编码字符的问题?有什么方法可以使用 php 重写文件吗?

谢谢。

编辑:

这是我的代码,它获取一个 xml 文件,然后根据基本 xml 文件中有多少不同的订单 ID 将其拆分成多个部分。上面的XML是这段代码处理过的XML之一,为什么这段代码返回的是那些带有编码字符的xml文件? (编码后的字符是 html 实体。)。

代码如下:

    <?php
// Prevent logging.
error_reporting(0);

$xmlFilepath = 'Final.xml';

function formatXML($xmlFilepath) {
$filename = 'info.txt';
$contents = file($filename);
$loadxml = simplexml_load_file($xmlFilepath);

//$key = $loadxml->order[2]->orderId;
$count = $loadxml->order;

// The [i] counter for $key(orderId + name of .xml files).

foreach($count as $plus){

$counter = $counter + 1;

}

for($i = 0; $i <= $counter; $i++){
$key = $loadxml->order[$i]->orderId;
$v = $i;
foreach($contents as $lines){
if ( preg_match_all( "/[^0-9]{$key}[^0-9]/", $lines ) ){

echo "{$key} found! <br>";

} elseif (preg_match_all( "/[^0-9]{$key}[^0-9]/", $lines) != true ) {

$x = false;

while($x == false) {

$orderset = $loadxml->order[$v];

// ORIGINAL
$formatxml = new SimpleXMLElement($orderset->saveXML());
$formatxml->saveXML("/home/floros24/public_html/farmaxml/orders/{$key}.xml"); // Save as {key.xml}.

// Open the file to get existing content
$current = file_get_contents($filename);
// Append a new person to the file
$current .= " {$key}";
// Write the contents back to the file
file_put_contents($filename, $current);

$x = true;

}
}
}
} // <--- Closing For[i].
} // <--- Closing of the Function.

formatXML($xmlFilepath);

?>

最佳答案

没有正确测试,但这可能有效。不知道你要找什么样的结果所以把它做成一个简单的关联数组

<?php

$content = 'your xml content';

$decode = static function(SimpleXMLElement $node) use (&$decode) {
if($node->children()->count() > 0) {
$result = [];

foreach($node->children() as $child) {
$result[$child->getName()] = $decode($child);
}
} else {
$result = html_entity_decode((string) $node);
}

return [$node->getName() => $result];
};

$xml = simplexml_load_string(
$content,
SimpleXMLElement::class,
LIBXML_NOERROR | LIBXML_ERR_NONE,
);

var_dump($decode($xml));

输出:

array(1) {
["order"]=>
array(16) {
["orderId"]=>
array(1) {
["orderId"]=>
string(4) "9132"
}
["statusId"]=>
array(1) {
["statusId"]=>
string(1) "3"
}
["adminUrl"]=>
array(1) {
["adminUrl"]=>
string(55) "www.floros24.gr/wp-admin/post.php?post=9132&action=edit"
}
["status"]=>
array(1) {
["status"]=>
string(24) "Ολοκληρωμένη"
}
["dateCreated"]=>
array(1) {
["dateCreated"]=>
string(25) "2021-06-26T22:24:56+03:00"
}
["dateCompleted"]=>
array(1) {
["dateCompleted"]=>
string(25) "2021-06-28T10:56:05+03:00"
}
["dateModified"]=>
array(1) {
["dateModified"]=>
string(0) ""
}
["datePaid"]=>
array(1) {
["datePaid"]=>
string(25) "2021-06-28T10:56:05+03:00"
}
["paymentMethodId"]=>
array(1) {
["paymentMethodId"]=>
string(1) "1"
}
["paymentMethod"]=>
array(1) {
["paymentMethod"]=>
string(24) "Αντικαταβολή"
}
["userId"]=>
array(1) {
["userId"]=>
string(2) "-1"
}
["notes"]=>
array(1) {
["notes"]=>
string(0) ""
}
["total"]=>
array(1) {
["total"]=>
string(5) "53.17"
}
["vatAmount"]=>
array(1) {
["vatAmount"]=>
string(5) "12.76"
}
["amount"]=>
array(1) {
["amount"]=>
string(5) "65.93"
}
["billing"]=>
array(1) {
["billing"]=>
array(14) {
["firstName"]=>
array(1) {
["firstName"]=>
string(18) "Ευαγγελία"
}
["lastName"]=>
array(1) {
["lastName"]=>
string(26) "Τριανταφύλλου"
}
["address"]=>
array(1) {
["address"]=>
string(24) "25ης Μαρτίου 23"
}
["city"]=>
array(1) {
["city"]=>
string(12) "Σίνδος"
}
["postcode"]=>
array(1) {
["postcode"]=>
string(5) "57400"
}
["state"]=>
array(1) {
["state"]=>
string(1) "B"
}
["country"]=>
array(1) {
["country"]=>
string(2) "GR"
}
["phone"]=>
array(1) {
["phone"]=>
string(10) "2310797958"
}
["cellphone"]=>
array(1) {
["cellphone"]=>
string(0) ""
}
["email"]=>
array(1) {
["email"]=>
string(16) "etrian@gmail.com"
}
["isInvoice"]=>
array(1) {
["isInvoice"]=>
string(1) "0"
}
["vat"]=>
array(1) {
["vat"]=>
string(0) ""
}
["taxOffice"]=>
array(1) {
["taxOffice"]=>
string(0) ""
}
["activity"]=>
array(1) {
["activity"]=>
string(0) ""
}
}
}
}
}

关于php - 如何使用 php 以正确的编码重写此 xml 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69618761/

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