소스 파일 : /yoyak/yoyak_work_temp.php (2018-05-23)     소스 설명 : (분류요약관리)
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
<?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;
}


Copyrightⓒ written by 차재복 (Cha Jae Bok)
"본 웹사이트 내 모든 저작물은 원출처를 밝히는 한 자유롭게 사용(상업화포함) 가능합니다"