Skip to content

Instantly share code, notes, and snippets.

@ochaochaocha3
Created June 10, 2014 14:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ochaochaocha3/4f4c48fb684477b012d4 to your computer and use it in GitHub Desktop.
Save ochaochaocha3/4f4c48fb684477b012d4 to your computer and use it in GitHub Desktop.
【PHP】どどんとふチャットログ HTML ファイルからチャットログ部分を抽出する
<?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