Last active
August 29, 2015 13:57
-
-
Save yasuken1990/9408353 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* 2014/03/07 author @yasuken1990 | |
* 使い方 | |
* $ php mtb_insert_create.php 引数1 引数2 | |
* 引数1 mtb_* | |
* 引数2 mtb_*.id がtextの場合、適当な文字を入れる a でも b でもなんでもいい | |
* | |
* $ php mtb_insert_create.php mtb_hoge | |
*/ | |
$path = $argv[1]; | |
// mtb_*.id が, textの場合は引数に適当な文字を入れてください | |
if (isset($argv[2])) { | |
$isText = true; | |
} | |
// currentディレクトリよりも下の階層にある場合TRUE | |
if (preg_match("#.*/(mtb.*)#misu", $path, $match)) { | |
$mtb = $match[1]; | |
} else { | |
$mtb = $path; | |
} | |
$insertData = file_get_contents($path); | |
$arrInsertData = preg_split("/\n/", $insertData); | |
$insertFile = ""; | |
$rank = 0; | |
foreach ($arrInsertData as $data) { | |
$arrData = preg_split("/\t/", $data); | |
if (isset($arrData[0]) && isset($arrData[1])) { | |
$id = $arrData[0]; | |
$name = $arrData[1]; | |
} else { | |
continue; | |
} | |
if (isset($isText)) { | |
$insertFile .= "INSERT INTO {$mtb} VALUES ('{$id}', '{$name}', {$rank});\n"; | |
} else { | |
$insertFile .= "INSERT INTO {$mtb} VALUES ({$id}, '{$name}', {$rank});\n"; | |
} | |
$rank++; | |
} | |
file_put_contents("{$mtb}_INSERT.sql", $insertFile); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment