Last active
October 25, 2018 04:04
-
-
Save fumikito/5496923ec71e4cc891f263029be79dec to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/usr/bin/env php | |
# 利用方法 | |
# 1. このファイルをホームフォルダなどのパスが通っている場所に maisu として保存します。 | |
# wget https://gist.githubusercontent.com/fumikito/5496923ec71e4cc891f263029be79dec/raw/6154450c6a547d14522a2ee7aa6ffd438fcfe3b7/maisu.php -O ~/bin/maisu | |
# 2. 実行権限を付与します。 | |
# chmod +x maisu | |
# 3. ファイルを指定して実行します。 | |
# maisu example.txt | |
# => 400字詰原稿用紙(20字×20行)換算で1枚(12行)です。 | |
# | |
# OPTION | |
# maisu file_name line_length(1行の文字数。デフォルト20) lines_per_page(1ページの行数。デフォルト20) | |
<?php | |
/** | |
* Display Error message. | |
* | |
* @param string $string | |
*/ | |
function error( $string ) { | |
die( '[Error]' . $srting . PHP_EOL ); | |
} | |
/** | |
* Display Error message. | |
* | |
* @param string $string | |
*/ | |
function line( $string ) { | |
echo $srting . PHP_EOL; | |
} | |
// Check variables | |
if ( 2 > count( $argv ) || ! file_exists( $argv[1] ) ) { | |
error( 'ファイルを指定してください。' ); | |
} | |
// Set line length. | |
$line_length = isset( $argv[2] ) && is_numeric( $argv[2] ) ? $argv[2] : 20; | |
// Set lines per 1 sheet. | |
$lines_per_sheet = isset( $argv[3] ) && is_numeric( $argv[3] ) ? $argv[3] : 20; | |
$line_count = 0; | |
foreach ( preg_split( '/\r?\n/u', file_get_contents( $argv[1] ) ) as $line ) { | |
$length = mb_strlen( $line, 'utf-8' ); | |
$line_count += ceil( $length / $line_length ); | |
} | |
$paper_count = ceil( $line_count / $lines_per_sheet ); | |
printf( '%d字詰原稿用紙(%d字×%d行)換算で%d枚(%d行)です。', $line_length * $lines_per_sheet, $line_length, $lines_per_sheet, $paper_count, $line_count ); | |
echo PHP_EOL; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment