如何从xml表中提取数组变量?

时间:2012-10-23 19:51:53

标签: mysql xml arrays extract

我希望有人可以帮助我。我需要从MySQL数据库中提取某些细节(“全名和姓氏”,“电子邮件”,“联系号码”),但无法弄清楚如何执行此操作。有问题的应用程序,“Grunion联系表单”,用于Wordpress保存数据库中的数据,如下所示:

Array
(
    [Full Name and Surname:] => Sharon Somerset
    [Email] => sharon@x.x.x
    [Contact Number:] => 08xxxxxx
    [Alternative Number] => 011xxxxxx
    [Please list convenient time to be contacted] => 
    [Medical Aid Quote] => Yes
    [Insurance Quote] => 
    [Policy Update] => 
    [Friend's Name and Surname:] => 
    [Friend's Contact Number:] => 
    [How did you find us?] => Google
)

每个条目都位于“longtext”字段内的自己的数据库行中。

如何从这样的几千行轻松提取“全名和姓氏”,“电子邮件”,“联系号码”数据,并重新保存在一个简单的表格或CSV文件中,以导入像phplist < / p>

1 个答案:

答案 0 :(得分:0)

这应该让你开始 - 它在文本字段上匹配简单的正则表达式。这需要一段时间才能完成 - 但它可以满足您的需求。

<?php
$string = "Array
(
    [Full Name and Surname:] => Sharon Somerset
    [Email] => sharon@x.x.x
    [Contact Number:] => 08xxxxxx
    [Alternative Number] => 011xxxxxx
    [Please list convenient time to be contacted] => 
    [Medical Aid Quote] => Yes
    [Insurance Quote] => 
    [Policy Update] => 
    [Friend's Name and Surname:] => 
    [Friend's Contact Number:] => 
    [How did you find us?] => Google
)";


$string = preg_replace("/Array\n\(\n/","",$string);
//remove first line

$string = preg_replace("/\)$/","",$string);
//remove last line

$lines = explode("\n",$string);
//split on new lines
print_r($lines);

foreach($lines as $l)
{
        preg_match("/\[(.+)\] => (.*)$/",$l,$matches);
        //$matches[1] will contain the label
        //$matches[2] will contain the value
        print_r($matches);

}
?>