php - 如何将文本文件中的特定数据导入mysql?

标签 php mysql

我从 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/

相关文章:

mysql catorgorize 和 when 和 else

mysql - Netbeans MySql 数据库连接

mysql - 与服务器中运行的 C/C++ 程序相比,MySQL 的速度有多快?

php - 不在 PHP 中将结果提取到数组中

javascript - 将远程 AngularJS 应用程序嵌入网页的正确方法是什么?

PHP - 从电子邮件下载 WAV

php - Ajax 来自 PHP 的多重回显

php - 使用uploadify最新版本上传视频

MySQL 查询没有得到准确的输出

php - MYSQL 需要建议将存储在 EST 时区数据库中的日期迁移到 UTC 时区