Skip to content

Instantly share code, notes, and snippets.

@bonjin6770
Last active December 31, 2015 20:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bonjin6770/8039306 to your computer and use it in GitHub Desktop.
Save bonjin6770/8039306 to your computer and use it in GitHub Desktop.
Simplenoteで出力したテキストファイルを分割するスクリプトです。
<?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