分類付き m_sakumotsu

このトピックの投稿一覧へ

なし 分類付き m_sakumotsu

msg# 1
depth:
0
前の投稿 - 次の投稿 | 親投稿 - 子投稿.1 .2 | 投稿日時 2010.11.19 02:11 | 最終変更
OhYeah!  管理人   投稿数: 983 オンライン
kabe さんの書かれていた FAMIC の作物概念図から、m_sakumotsu テーブルを作成するためのクエリーを自動生成するスクリプトを作りました。
まだ、HTML から SQL へ変換する部分だけで、概念図の更新チェックなどはこれからです。ふりがなや別名にも対応していません。

とりあえずは、下記 URL でお試しください。
http://macs.o-ya.net/data/m_sakumotsu.php

<?php
$url = 'http://www.acis.famic.go.jp/ddata/sakumotugainen.htm';

mb_internal_encoding('utf8');
header('Content-Type: text/plain; charset=Shift_JIS');
echo <<<EOM
--/d
drop table if exists m_sakumotsu;
create table m_sakumotsu (idsaku integer primary key autoincrement, id1 integer, id2 integer, id3 integer, id4 integer, id5 integer, sakumotsu varchar);
begin transaction;

EOM;

$trs = explode('</tr>', str_replace(array("\r", "\n"), '', mb_convert_encoding(file_get_contents($url), 'utf8', 'auto')));
array_shift($trs);
while ($tr = array_shift($trs)) {
  if (strpos('階層', $tr) > 0) continue;
  $tds = preg_split('{</td>}i', $tr);
  if (count($tds) <= 2) continue;
  $level = 0;
  while ($td = array_shift($tds)) {
    $str = preg_replace('/.*>/', '', $td);
    if ($str == '│') {
      $level++;
      continue;
    }
    if ($str == '') {
      if (preg_match('/colspan=[\'\"]?([0-9]+)[\'\"]?/', $td, $matches)) {
        $level += $matches[1];
      } else {
        $level++;
      }
      continue;
    }
    if ($str == '├' || $str == '└') {
      $id[$level]++;
      for($i = $level + 1; $i <= 4; $i++) $id[$i] = 0;
      $td = array_shift($tds);
      $item = mb_convert_encoding(mb_convert_kana(preg_replace('/.*>/', '', $td), 'KVA'), 'sjis-win', 'utf8');
      echo sprintf("insert into m_sakumotsu (id1, id2, id3, id4, id5, sakumotsu) values (%d, %d, %d, %d, %d, '%s');\n", $id[0], $id[1], $id[2], $id[3], $id[4], $item);
      break;
    }
  }
}
echo "commit;\n";
?>


投票数:3 平均点:3.33

投稿ツリー

  条件検索へ