うまくいけば、これは助けになるでしょう。
まず、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 . "')");
}
?>
幸運を!