有这个csv file,我想在我的MySQL数据库中导入它,只需键入一个邮政编码即可绘制我想要的所有多边形。 在这个文件中我有
我的第一个问题是如何解析此文件以将数据保存在我的数据库中以包含3列
|postcode|latitude|longitude
请帮忙解决这个问题?如果你知道,或者建议我一个更好的方法。
感谢。
编辑:我每次尝试使用这个PHP代码编辑
$fname = "uk.csv";
$fhandle = fopen($fname,"r");
$content = fread($fhandle,filesize($fname));
$content = str_replace('"<MultiGeometry><Polygon><outerBoundaryIs><LinearRing><coordinates>', '', $content);
$fhandle = fopen($fname,"w");
fwrite($fhandle,$content);
fclose($fhandle);
但是这个文件是同类的,因为并非所有行都以相同的字符串
开头<MultiGeometry><Polygon><outerBoundaryIs><LinearRing><coordinates>
但也只有
<Polygon><outerBoundaryIs><LinearRing><coordinates>
答案 0 :(得分:1)
这不仅仅是一个字符串,它是有效的XML。
使用DOM方法解析String。
简单的例子:
//create DOMDocument
$doc=new DOMDocument();
//load the source from string
$doc->loadXML('<MultiGeometry><Polygon><outerBoundaryIs><LinearRing><coordinates>0.167014,51.79976,0.0 0.167634,51.79976,0.0 0.177091,51.796733,0.0 0.181105,51.793738,0.0</coordinates></LinearRing></outerBoundaryIs></Polygon></MultiGeometry>');
//fetch the coordinates-nodes
$coords=$doc->getElementsByTagName('coordinates');
//get the content of the first <coordinates/>
echo $coords->item(0)->nodeValue;
//returns:
//0.167014,51.79976,0.0 0.167634,51.79976,0.0 0.177091,51.796733,0.0 0.181105,51.793738,0.0