【问题标题】:Open txt file and insert it's content into table accordingly打开txt文件并将其内容相应地插入表中
【发布时间】:2015-01-26 09:01:09
【问题描述】:

我想读取文本文件的内容并将内容插入到表格中 这是文本文件的内容

--------------------------------
Text file contents
--------------------------------
[playlist]
mode=play
Title1=Radio 1|United Kingdom                                                              
File1=http://www.test.com/0001                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
Title2=Radio 2 |United States                                                                        
File2=http://www.test.com/0002                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
Title3=Radio 3|United Kingdom                                                                          
File3=http://www.test.com/0003                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
NumberOfEntries=3
Version=2
-------------------------------

并存储在这样的表中

  id  |     item                |   url
----------------------------------------------------------
  1   | Radio 1|United Kingdom  | http://www.test.com/0001 
  2   | Radio 2 |United States  | http://www.test.com/0002 
  3   | Radio 3|United Kingdom  | http://www.test.com/0003 

我如何获取标题和文件,以便我可以将其插入数据库表。 我正在使用 file_get_contents() 来读取文本文件。

我当前的代码:

$content = file_get_contents("textfile.txt");

$lines = explode("\n", $content);

foreach ($lines as $line) {
    $row = explode("", $line);


    $stmt="INSERT INTO table_1 (item, url)
    VALUES
    (....)";

}

请帮助我。 提前致谢。

【问题讨论】:

  • 您需要使用 preg_match_all 而不是explode来提取标题和文件。
  • 你应该阅读正则表达式,然后这样做
  • 请帮助我该怎么做

标签: php file-get-contents


【解决方案1】:

如果文件总是相同的格式,你可以解析数据并构建一个数组

$content = file_get_contents('textfile.txt');
$lines = explode("\n", $content);
$data = array();
foreach ($lines as $line) {
    if (preg_match('/^(Title|File)([0-9]+)\=(.+)$/', $line, $match)) {
        $data[$match[2]][$match[1]] = $match[3];
    }
}

之后,您可以循环遍历数据数组并插入到数据库中。

foreach($data as $id => $values) {
    $sql = 'INSERT INTO table_1 (item, url) VALUES ("' .    $values['Title'] . '", "'  . $values['File'] . '")';//The values need to be escaped!
}

【讨论】:

  • 非常感谢,我会用你的方法试一试
  • 它给了我乱码数据 :( OUTPUT => mRhUhUhUhUhUhUhPhPhPhPhRhWhMhShWhChShIhLhBhRhChFh2hThRhNhShRhShRhShShRhWhGhThWhAhRhPhFhRhGhNhPhihLhIh52
  • 这根本没有任何意义,你可以编辑你的帖子并在$content = file_get_contents("textfile.txt");$lines = explode("\n", $content);之后添加var_dump($lines);的输出
  • 感谢您回复我 var_dump($lines) 给了我正确的内容,但现在页面变为空白,数据未插入 DB 表,也没有显示任何错误。对不起,我是个菜鸟:(
  • 你能把var_dump的输出贴出来吗?你能不能把$data的var_dump也贴在我代码的第一部分之后?
猜你喜欢
  • 2020-03-24
  • 1970-01-01
  • 2022-11-12
  • 2017-12-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多