<?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;
}
?>