【PHP】どどんとふチャットログ HTML ファイルからチャットログ部分を抽出する
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 | |
/** | |
* どどんとふチャットログ HTML ファイルからチャットログ部分を取り出して返す | |
* @param string $filename ファイル名 | |
* @return string チャットログ部分を含む HTML コード | |
* @return NULL 読み込みに失敗した場合 | |
*/ | |
function read_dodontof_chat_log($filename) { | |
# ファイルポインタを取得する | |
$handle = fopen($filename, 'r'); | |
if ($handle === FALSE) return NULL; | |
$html_code = ''; | |
# 必要な部分が出現するまで読み飛ばす | |
$body_start = '/^\s*<body>/'; | |
while (($line = fgets($handle)) !== FALSE) { | |
if (preg_match($body_start, $line)) break; | |
} | |
# 必要な部分だけ読み込む | |
$body_end = '|^\s*</body>|'; | |
while (($line = fgets($handle)) !== FALSE) { | |
# 必要な部分の外に出たら脱出する | |
if (preg_match($body_end, $line)) break; | |
$html_code .= $line; | |
} | |
# ファイルを閉じる | |
fclose($handle); | |
return $html_code; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment