Php 从Wikipedia提取表数据并转换为XML文档
第页: 是否可以提取以下各项:Php 从Wikipedia提取表数据并转换为XML文档,php,xml,Php,Xml,第页: 是否可以提取以下各项: 货币代码 货币所有权 货币位置 如果可能的话,保存到XML文档中,如下所示: <currency> <AED> <curr>United Arab Emirates dirham</curr> <loc>United Arab Emirates</loc> </AED> </currency> <currency
- 货币代码
- 货币所有权
- 货币位置
<currency>
<AED>
<curr>United Arab Emirates dirham</curr>
<loc>United Arab Emirates</loc>
</AED>
</currency>
<currency>
<AFN>
<curr>Afghan afghani</curr>
<loc>Afghanistan</loc>
</AFN>
</currency>
谢谢。表格已创建,因此可在wiki格式上使用:
您可以编写一个脚本,将wiki格式解析为一个数组,并从中构建XML。尝试按换行符拆分字符串(例如,使用分解
),然后按分隔表列的|
拆分每行
大概是这样的:
$currencyList = array();
$source = "<insert wikipedia table code here>";
$rows = explode("\n", $source); // split the table in rows
foreach($rows as $row) {
if(strlen(trim($row)) < 0) { continue; } // ignore empty rows
if(trim($row) == "|-") { continue; } // ignore table line separators
$row = substr($row, 2); // remove the "| " from the beginning of each row
$cols = explode("||", $row); // split the row in columns
$currency = array( // clean data and store in associative array
'code' => trim($cols[0]),
'number' => trim($cols[1]),
'digits_after_decimal' => trim($cols[2]),
'name' => trim($cols[3])
);
array_push($currencyList, $currency); // add the read currency to the list
}
var_dump($currencyList); // $currencyList now has a list of associative arrays with your data.
$currencyList=array();
$source=“”;
$rows=分解(“\n”,$source);//将表拆分为行
foreach($行作为$行){
如果(strlen(trim($row))<0{continue;}//忽略空行
if(trim($row)=“|-”{continue;}//忽略表格行分隔符
$row=substr($row,2);//删除每行开头的“|”
$cols=explode(“| |,$row);//将行拆分为列
$currency=array(//清除数据并存储在关联数组中
'code'=>trim($cols[0]),
'number'=>trim($cols[1]),
“小数点后的数字”=>trim($cols[2]),
'name'=>trim($cols[3])
);
数组_push($currencyList,$currency);//将读取的货币添加到列表中
}
变量转储($currencyList);//$currencyList现在有一个与数据关联的数组列表。
要构建XML,可以尝试PHP