如何拆分Mysql字符串数据字段值(PHP)并将结果导出为XML

时间:2014-08-25 13:02:45

标签: php mysql xml string split

如何拆分Mysql字符串数据字段值并将结果导出为XML:

(输入/存储的数据是:洗碗机冰箱;冷冻气体;电磁炉;)

实际当前的工作代码是:

if ($row->appliances){
$appliances = str_replace(";", " ",$row->appliances )."";
} else {
$appliances = "";

目前产生的XML输出为:

<.appliances.>Dishwasher Fridge Freezer Gas Cooker<./appliances.>

所需的结果XML结果是:

.<.appliance1>Dishwasher<./appliance1>

.<.appliance2>Fridge Freezer<./appliance2>

.<.appliance3>Gas Cooker<./appliance3>

干杯

1 个答案:

答案 0 :(得分:0)

而不是str_replace使用explode -

if ($row->appliances){
    $appliances = explode(";", $row->appliances ); // $appliances is now an array
    for($i = 0; $i < count($appliances) - 1; $i++) { // loop through the array
        // format your XML here, $appliances[$i] will be the current appliance
    }
} else {
    $appliances = "";
}