希望这会有所帮助。
首先,您需要查看与 PHP 关联的文件库。
参考:http ://www.php.net/manual/en/ref.filesystem.php
使用 fopen 和 fread,您可以打开有问题的文件并从那里解析它。
<?php
// get contents of a file into a string
$filename = "something.txt";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);
?>
接下来,我们将使用一些简单的字符串操作来获取您的重要信息。使用split
,我们可以将您的文件内容切割成好东西。
参考: http: //php.net/manual/en/function.split.php
<?php
// sanitize content headers
$contents = split("<\?\?", $contents);
foreach($contents as $content) {
// remove content footers
str_replace("??>", "", $content);
}
?>
最后,我们将遍历刚刚使用 split 创建的数组中的所有元素,并将它们插入到我们的数据库中。
参考: http: //php.net/manual/en/book.mysql.php
<?php
// sanitize content headers
$contents = split("<\?\?", $contents);
foreach($contents as $content) {
if (empty($content)) {
continue;
}
// remove content footers
str_replace("??>", "", $content);
// insert into database
mysql_query("INSERT INTO `something` VALUES ('" . $content . "')");
}
?>
总体而言,最终代码应如下所示:
<?php
// get contents of a file into a string
$filename = "something.txt";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);
// sanitize content headers
$contents = split("<\?\?", $contents);
foreach($contents as $content) {
if (empty($content)) {
continue;
}
// remove content footers
str_replace("??>", "", $content);
// insert into database
mysql_query("INSERT INTO `something` VALUES ('" . $content . "')");
}
?>
祝你好运!