我从 dbpedia 下载了一个文件,内容如下:
<http://dbpedia.org/resource/Selective_Draft_Law_Cases> <http://dbpedia.org/ontology/wikiPageExternalLink> <http://supreme.justia.com/cases/federal/us/245/366/> .
<http://dbpedia.org/resource/List_of_songs_recorded_by_Shakira> <http://dbpedia.org/ontology/wikiPageExternalLink> <http://www.shakira.com/> .
<http://dbpedia.org/resource/Bucharest_Symphony_Orchestra> <http://dbpedia.org/ontology/wikiPageExternalLink> <http://www.symphorchestra.ro/> .
<http://dbpedia.org/resource/Bucharest_Symphony_Orchestra> <http://dbpedia.org/ontology/wikiPageExternalLink> <http://symphorchestra.ro> .
<http://dbpedia.org/resource/Bucharest_Symphony_Orchestra> <http://dbpedia.org/ontology/wikiPageExternalLink> <http://www.youtube.com/symphorchestra> .
我需要从每行的第一部分提取标题(即第一行中的Selective_draft_Law_Cases
,第二行中的List_of_songs_etc等))并将其与URL一起保存在mysql表中是同一行中的第三个元素,即 first line
对于 second line等等
我还需要跳过文件中包含不同的、不相关信息的第一行。
在 PHP 中完成此操作的最快方法是什么?
注意:该文件相当大(大小超过 1 GB,超过 600 万行)。
提前致谢!
最佳答案
我确信它可以优化,但这只是一个开始。尝试:
function insertFileToDb(){
$myFile = "myFile.txt"; //your txt file containing the data
$handle = fopen($myFile, 'r');
//Read first line, but do nothing with it
$contents = fgets($handle);
//now read the rest of the file line by line
while(!feof($handle)){
$data = fgets($handle);
//remove <> characters
$vowels = array("<", ">");
$data = str_replace($vowels, "", $data);
//remove spaces to a single space for each line
$data = preg_replace('!\s+!', ' ', $data);
/*
* Get values from array, 1st URL is $dataArr[0] and 2nd URL is $dataArr[2]
* Explode on ' ' spaces
*/
$dataArr = explode(" ", $data);
//Get last part of uri from 1st element in array
$title = $this->getLastPartOfUrl($dataArr[0]);
//Execute your sql query with $title and $dataArr[2] which is the url
INSERT INTO `table` ...
}
fclose($handle);
}
function getLastPartOfUrl($url){
$keys = parse_url($url); // parse the url
$path = explode("/", $keys['path']); // splitting the path
$last = end($path); // get the value of the last element
return $last;
}
关于php - 如何将文本文件中的特定数据导入mysql?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14814647/