<?php // (2018.5.23, 차재복, Cha Jae Bok, http://www.ktword.co.kr)
# db 접속
// 사용자 정의 상수에 의해 db 접근 변수 설정
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_USER', 'root');
DEFINE ('DB_PASSWORD', 'cjb1234');
DEFINE ('DB_NAME', 'dict');
// db 접속
include_once "../base_utils/config.php";
# 쿼리문
$query = "select tree_id,detail,detail_abbr from map_word "; // where tree_id=1194 or tree_id=1336
$result = mysqli_query($dbi,$query);
if (mysqli_errno($dbi)) { echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n";}
while ($matched = mysqli_fetch_assoc($result)) {
// 상세 detail 비어있으면 지나침
if (empty($matched[detail]) or empty($matched[tree_id])) continue;
// 기존 id 있으면, 삭제
mysqli_query($dbi,"delete from detail where id=$matched[tree_id]");
if (mysqli_affected_rows($dbi) > 0) echo "<br>기존 id 가 있어서, 삭제 함 !!! <br>";
// 단위 구절마다 분리 추출하여 배열화시키는 함수 호출
$substr = seperate($matched[detail]);
echo "현재 sub_id 갯수 : ".count($substr)."(id:{$matched[tree_id]}번)<br>";
// 배열화된 소항목별로 detail 테이블에 매 레코드 삽입
foreach($substr as $key => $value) {
$title = strip_tags($value);
$title = trim( strstr($title,PHP_EOL,true) );
$title = str_replace(array('1. ','2. ','3. ','4. ','5. ','6. ','7. ','8. ','9. ','10. '),'',$title);
$content = trim( strstr($value,PHP_EOL,false) );
$content = str_replace(' ㅇ ','ㅇ ',$content);
mysqli_query($dbi,"insert into detail (id,sub_id,sub_name,detail) values ($matched[tree_id],$key,'{$title}','{$content}')");
if (mysqli_errno($dbi)) { echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n";}
// if (mysqli_affected_rows($dbi)>0) echo "레코드 1개 삽입됨 <br>";
}
}
$result = mysqli_query($dbi,"select * from detail");
if (mysqli_errno($dbi)) { echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n";}
echo "<br>현재 레코드 총 갯수 : ".mysqli_num_rows($result);
// 단위 구절마다 분리 추출하여 배열화시키는 함수
function seperate($abbr) {
// 용어해설 설명 단위 구절 마다 추출
$rest = $abbr;
$i=1;
while ( ($pos = strpos($rest,PHP_EOL.($i+1).". ")) !== false ) {
$first = ( $i==1 ? 0 : 2 );
$len = $pos - $first + 1;
$substr[$i] = trim(substr($rest, $first, $len));
$rest = substr($rest, $pos);
$substr[$i+1] = trim($rest);
$i += 1;
}
if ($i <= 1) {
$substr[1] = $rest;
}
return $substr;
}