Last active
December 31, 2015 20:19
-
-
Save bonjin6770/8039306 to your computer and use it in GitHub Desktop.
Simplenoteで出力したテキストファイルを分割するスクリプトです。
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 | |
// simplenote-export.txtのパス | |
$simplenoteExportFilePath = "simplenote-export.txt"; | |
// テキストファイルの読み込み | |
$fp = fopen($simplenoteExportFilePath,"r"); | |
$readedText = array(); | |
while(!feof($fp)){ | |
$load = fgets($fp); | |
if(!$load){ | |
break; | |
} | |
// echo $load; | |
array_push($readedText, $load); | |
} | |
fclose($fp); | |
// 読み込んだ文字列の分割処理 | |
$addTextFilePath = ""; | |
$textFiles = array(); | |
$textFileValue = array(); | |
// 配列分だけループ処理 | |
foreach($readedText as $text){ | |
//テキストファイルの最後を検出 | |
$subText = substr($text,0,3); | |
if(($subText === '---') && (strlen(rtrim($text)) == 3)){ | |
//$textFIlesに配列を追加する。 | |
array_push($textFiles, $textFileValue); | |
//$textFileValueを初期化する。 | |
$textFileValue = array(); | |
continue; | |
} | |
array_push($textFileValue, $text); | |
} | |
// テキストファイルへの書き出し | |
foreach($textFiles as $textFile){ | |
// 配列先頭にある改行を削除する | |
$textFile[0] = rtrim($textFile[0]); | |
if(empty($textFile[0])){ | |
array_shift($textFile); | |
} | |
// テキストファイルの作成 | |
$addTextFilePath = rtrim($textFile[0]) . '.txt'; | |
touch($addTextFilePath); | |
chmod($file_name, 0777); | |
$fp = fopen($addTextFilePath,'a'); | |
foreach ($textFile as $addText){ | |
fputs($fp,$addText); | |
} | |
fclose($fp); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment