소스 파일 : /reform/file_transform.php (2019-02-09)     소스 설명 : (개선중) 텍스트 파일 내 단어들 중 `정보통신용어해설`과 일치하면 링크를 줌
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
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php // (2019.2.9, 차재복, Cha Jae Bok, http://www.ktword.co.kr)

# 세션 설정
	session_start(); // 세션 스타트 (매 웹페이지 마다 필요)

# styling
	echo "<style type='text/css'>
				body { line-height: 150%; font-size: 13px; }
			</style>";



# db 접속
	include "../base_utils/db_conn.php";


# 관련 파일들 일괄 변환
	$files = array('reform_detail.php','../coding/coding.php','coding.txt','op_env.txt','op_files.txt');
	foreach($files as $key => $value) {
//		$out_file = strstr($value,'.',true).".2nd";
		$out_file = substr($value,0,strrpos($value,'.')).".2nd";
		if ( filemtime("./{$value}") > filemtime("./{$out_file}") ) { 
			$string = file_get_contents("./{$value}");
			$out_string = file_transform($string,$dbi);
			file_put_contents("./{$out_file}",$out_string);
			// 최종 텍스트 파일별 처리 결과
			echo "<pre>";
				echo htmlspecialchars($out_string);
			echo "</pre>";
			echo "<hr>";
		}
	}


function file_transform($string,$dbi) {

	# 쿼리문 생성 및 쿼리 요청
	$query = "select no,word,length(word) as len from dict_word_list group by word having count(*)<2 order by len desc";
	$result = mysqli_query($dbi,$query);
		if (mysqli_errno($dbi)) { echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n";}

	# 쿼리 결과에 따라, 각 레코드별 단어 링크 처리
	$count = $count+1;
	while ($matched = mysqli_fetch_assoc($result)) {

			word_replace($string, $matched[word], $matched[no], $count);

			$space_cnt = substr_count($matched[word],' ');
			if ($space_cnt>0) {
				$space_removed_word = str_replace(' ','',$matched[word]);
				word_replace($string, $space_removed_word, $matched[no], $count);
			} 

	}
	return $string;

}

/*
	echo "정보통신기술용어와의 일치 용어를 링크 교체한 결과 : {$count}개";
//	file_put_contents("./{$out_file}",$string);
	echo "<hr>";

	echo "<pre>";
//		echo htmlspecialchars($string);
//		echo $string;
	echo "</pre>";
	echo "<hr>";
*/


function word_replace(&$string, $word, $no, &$count) {

		while ( $left=mb_strpos($string, $word, $left) ) {

			$len = mb_strlen($word);

			if (is_skip($string,$word,$left)) {
				$left += $len;
				continue;
			}

			$right = $left + mb_strlen($word);
//			$replaced = '{'.$word.'@'.$count.'}';
			$replaced = "<a href='/word/abbr_view.php?m_temp1={$no}'>".strip_bracket($word)."</a>";
			$string = mb_substr($string,0,$left).$replaced.mb_substr($string,$right);

			$count += 1;
			$left = $left + mb_strlen($replaced);
		}

}


function is_skip($string, $word, $pos) {
	$ok_char = [' ',',','/',':','(',')','[',']','"',"'","\n","\r","\t"];
	$doubt_char = ['<','>'];
	$before_char = mb_substr($string,$pos-1,1);
	$after_char = mb_substr($string,$pos+mb_strlen($word),1);
	$before_two_char = mb_substr($string,$pos-2,2);
	$after_four_char = mb_substr($string,$pos+mb_strlen($word),4);
	if( in_array($before_char,$ok_char) and in_array($after_char,$ok_char) ) {
		return false; // no skip
	} else if ( $before_two_char == "'>" and $after_four_char == "</a>") {
		return true; // skip
	} else if ( in_array($before_char,$ok_char) and $after_char == '<' ) {
		if ($after_four_char != '</a>') return false; // no skip
	} else if ( $before_two_char != "'>" and in_array($after_char,$ok_char) ) {
		return false; // no skip
		
		/*		 
		in_array($before_char,$doubt_char) or 
		$before_two_char = mb_substr($string,$pos-2,2);
		*/
		/*
		$left = mb_strrpos($string,'<a ',$pos);
		$center = mb_strrpos($string,'\'>',$pos);
		$right = mb_strpos($string,'</a>',$pos);
		$right_chk =  mb_strpos($string,'<a ',$pos);
		if ( $left < $pos and ($center > $left and $center < $pos) and $right > $pos ) {
			return true; // skip
		} else {
			return false; // no skip
		}
		*/
	} else {
		return true; // skip
	}
}


function html_tag_replace($string, $first="<a href='", $middle="'>", $end="</a>") {

	$count = 1;
	while ( ($left=strpos($string,$first,$right)) and ($center=strpos($string,$middle,$left)) and ($right=strpos($string,$end,$center)) ) {

		$extract_1st = substr($string,$left+strlen($first),$center-$left-strlen($first));
		
		$extract_2nd = substr($string,$center+strlen($middle),$right-$center-strlen($middle));
		if (strpos($extract_2nd,$first)!==false or strpos($extract_2nd,$middle)!==false) {
			$right = $left + strlen($first);
			continue;
		}

		$array[] = array($count, $extract_2nd, $extract_1st);

//		$replaced = '{'.$extract_2nd.'@'.$count.'}';
		if (strpos($extract_1st,'/word/abbr_view.php?')!==false) {
			$replaced = $extract_2nd;
		} else {
			$replaced = '{@'.$count.'}';
		}
		$string = substr($string,0,$left).$replaced.substr($string,$right+strlen($end));

		$count += 1;
		$right = $left+strlen($replaced);
	}
	return array($string,$array);
}


function strip_bracket($str) {
	$rest = $str;
	while ( ($start = strpos($rest,'[')) and ($end = strpos($rest,']',$start+1)) ) {
		$rest = substr($rest,0,$start) . substr($rest,$end+1);
	}
	return $rest;
}

?>


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