You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

140 lines
4.4 KiB

7 years ago
  1. <?php
  2. defined('BASEPATH') OR exit('No direct script access allowed');
  3. /**
  4. * Form Validation Class
  5. *
  6. * @package CodeIgniter
  7. * @subpackage Libraries
  8. * @category Validation
  9. * @author EllisLab Dev Team
  10. * @link <a href="https://codeigniter.com/user_guide/libraries/form_validation.html" target="_blank">https://codeigniter.com/user_guide/libraries/form_validation.html</a>
  11. */
  12. class WB_Form_validation extends CI_Form_validation {
  13. function __construct() {
  14. parent::__construct();
  15. }
  16. public function error($field, $prefix = '', $suffix = '')
  17. {
  18. if (empty($this->_field_data[$field]['error']))
  19. {
  20. return '';
  21. }
  22. if ($prefix === '')
  23. {
  24. $prefix = $this->_error_prefix;
  25. }
  26. if ($suffix === '')
  27. {
  28. $suffix = $this->_error_suffix;
  29. }
  30. return $this->josa_conv($prefix.$this->_field_data[$field]['error'].$suffix);
  31. }
  32. public function error_string($prefix = '', $suffix = '')
  33. {
  34. // No errors, validation passes!
  35. if (count($this->_error_array) === 0)
  36. {
  37. return '';
  38. }
  39. if ($prefix === '')
  40. {
  41. $prefix = $this->_error_prefix;
  42. }
  43. if ($suffix === '')
  44. {
  45. $suffix = $this->_error_suffix;
  46. }
  47. // Generate the error string
  48. $str = '';
  49. foreach ($this->_error_array as $val)
  50. {
  51. if ($val !== '')
  52. {
  53. $str .= $prefix.$val.$suffix."\n";
  54. }
  55. }
  56. // return $str;
  57. return $this->josa_conv($str);
  58. }
  59. /**
  60. * 문장에서 조사를 적절하게 변환시킵니다.
  61. */
  62. public function josa_conv($str)
  63. {
  64. $josa = '이가은는을를과와';
  65. return preg_replace_callback("/(.)\\{([{$josa}])\\}/u",
  66. function($matches) use($josa) {
  67. // 조사 바로 앞 한글자
  68. $lastChar = $matches[1];
  69. $postpositionMatched = $matches[2];
  70. $arrPostposition = array(
  71. 'N' => $postpositionMatched,
  72. 'Y' => $postpositionMatched
  73. );
  74. $pos = mb_strpos($josa, $postpositionMatched);
  75. if ($pos % 2 != 0) {
  76. $arrPostposition['Y'] = mb_substr($josa, $pos-1, 1);
  77. } else {
  78. $arrPostposition['N'] = mb_substr($josa, $pos+1, 1);
  79. }
  80. // 기본값 : 종성있음
  81. $lastCharStatus = 'Y';
  82. // 2바이트 이상 유니코드 문자
  83. if (strlen($lastChar) > 1) {
  84. switch (strlen($lastChar)) {
  85. case 1:
  86. $lastchar_code = ord($lastChar);
  87. break;
  88. case 2:
  89. $lastchar_code = ((ord($lastChar[0]) & 0x1F) << 6) | (ord($lastChar[1]) & 0x3F);
  90. break;
  91. case 3:
  92. $lastchar_code = ((ord($lastChar[0]) & 0x0F) << 12) | ((ord($lastChar[1]) & 0x3F) << 6) | (ord($lastChar[2]) & 0x3F);
  93. break;
  94. case 4:
  95. $lastchar_code = ((ord($lastChar[0]) & 0x07) << 18) | ((ord($lastChar[1]) & 0x3F) << 12) | ((ord($lastChar[2]) & 0x3F) << 6) | (ord($lastChar[3]) & 0x3F);
  96. break;
  97. default:
  98. $lastchar_code = $lastChar;
  99. }
  100. $code = $lastchar_code - 44032;
  101. // 한글일 경우 (가=0, 힣=11171)
  102. if ($code > -1 && $code < 11172) {
  103. // 초성
  104. //$code / 588
  105. // 중성
  106. //$code % 588 / 28
  107. // 종성
  108. if ($code % 28 == 0) $lastCharStatus = 'N';
  109. }
  110. // 1바이트 ASCII
  111. } else {
  112. // 숫자중 2(이),4(사),5(오),9(구)는 종성이 없음
  113. if (strpos('2459', $lastChar) > -1) {
  114. $lastCharStatus = 'N';
  115. }
  116. }
  117. return $lastChar.$arrPostposition[$lastCharStatus];
  118. }, $str
  119. );
  120. }
  121. }