PageRenderTime 58ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/admin/includes/functions/extra_functions/functions_chn.php

https://github.com/happyxlq/zencart_svn
PHP | 598 lines | 479 code | 58 blank | 61 comment | 204 complexity | c9976ff0408a6a11afd2c6446fad11bf MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1
  1. <?php
  2. /**
  3. * functions_chn.php
  4. * Simplified Chinese character conversion
  5. *
  6. * @package functions
  7. * @copyright Portions Copyright 2003 osCommerce
  8. * @license http://www.zen-cart.com/license/2_0.txt GNU Public License V2.0
  9. * @version $Id: functions_chn.php 00001 2006-09-03 20:00:00 jack $
  10. */
  11. /*中文处理工具函数
  12. --- 空格 ---
  13. string GBspace(string) --------- 每个中文字之间加空格
  14. string GBunspace(string) ------- 每个中文字之间的空格清除
  15. string clear_space(string) ------- 用来清除多余的空格
  16. --- 转换 ---
  17. string GBcase(string,offset) --- 将字符串内的中英文字转换大小写
  18. offset : "upper" - 字符串全转为大写 (strtoupper)
  19. "lower" - 字符串全转为小写 (strtolower)
  20. "ucwords" - 将字符串每个字第一个字母改大写 (ucwords)
  21. "ucfirst" - 将字符串第一个字母改大写 (ucfirst)
  22. string GBrev(string) ----------- 颠倒字符串
  23. --- 文字检查 ---
  24. int GB_check(string) ----------- 检查字符串内是否有 GB 字,有会返回 true,
  25. 否则会返回false
  26. int GB_all(string) ------------- 检查字符串内所有字是否有 GB 字,是会返回 true,
  27. 否则会返回false
  28. int GB_non(string) ------------- 检查字符串内所有字并不是 GB 字,是会返回 true,
  29. 否则会返回false
  30. int GBlen(string) -------------- 返回字符串长度(中文字只计一字母)
  31. --- 查找、取代、提取 ---
  32. int/array GBpos(haystack,needle,[offset]) ---- 查找字符串 (strpos)
  33. offset : 留空 - 查找第一个出现的位置
  34. int- 由该位置搜索出现的第一个位置
  35. "r"- 查找最后一次出现的位置 (strrpos)
  36. "a"- 将所有查找到的字储存为数组(返回 array)
  37. string GB_replace(needle,str,haystack) -- 查找与取代字符串 (str_replace)
  38. string GB_replace_i(needle,str_f,str_b,haystack) -- 不检查大小写查找与取代字符串
  39. needle - 查找字母
  40. str - 取代字母 ( str_f - 该字母前, str_b 该字母后)
  41. haystack - 字符串
  42. string GBsubstr(string,start,[length]) -- 从string提取出由开始到结尾或长度
  43. length的字符串。
  44. 中文字只计一字母,可使用正负数。
  45. string GBstrnear(string,length) -- 从 string提取最接近 length的字符串。
  46. length 中 中文字计2个字母。
  47. --- 注意 ---
  48. 如使用由 Form 返回的字符串前,请先替字符串经过 stripslashes() 处理,除去多余的 \ 。
  49. 用法:在原 PHP 代码内加上:
  50. include ("GB.inc");
  51. 即可使用以上工具函数。
  52. */
  53. function GBlen($string) {
  54. $l = strlen($string);
  55. $ptr = 0;
  56. $a = 0;
  57. while ($a < $l) {
  58. $ch = substr($string,$a,1);
  59. $ch2 = substr($string,$a+1,1);
  60. if (ord($ch) >= HexDec("0x81") && ord($ch2) >= HexDec("0x40")) {
  61. $ptr++;
  62. $a += 2;
  63. } else {
  64. $ptr++;
  65. $a++;
  66. } // END IF
  67. } // END WHILE
  68. return $ptr;
  69. }
  70. function GBsubstr($string,$start,$length) {
  71. if (!is_int($length) && $length != "") {
  72. return "错误:length 值错误(必须为数值)。<br>";
  73. } elseif ($length == "0") {
  74. return "";
  75. } else {
  76. $l = strlen($string);
  77. $a = 0;
  78. $ptr = 0;
  79. $str_list = array();
  80. $str_list2 = array();
  81. while ($a < $l) {
  82. $ch = substr($string,$a,1);
  83. $ch2 = substr($string,$a+1,1);
  84. if (ord($ch) >= HexDec("0x81") && ord($ch2) >= HexDec("0x40")) {
  85. $str_list[$ptr] = $a;
  86. $str_list2[$ptr] = $a+1;
  87. $ptr++;
  88. $a += 2;
  89. } else {
  90. $str_list[$ptr] = $a;
  91. $str_list2[$ptr] = $a;
  92. $ptr++;
  93. $a++;
  94. } // END IF
  95. } // END WHILE
  96. if ($start > $ptr || -$start > $ptr) {
  97. return;
  98. } elseif ($length == "") {
  99. if ($start >= 0) { // (text,+)
  100. return substr($string,$str_list[$start]);
  101. } else { // (test,-)
  102. return substr($string,$str_list[$ptr + $start]);
  103. }
  104. } else {
  105. if ($length > 0) { // $length > 0
  106. if ($start >= 0) {// (text,+,+)
  107. if (($start + $length) >= count($str_list2)) {
  108. return substr($string,$str_list[$start]);
  109. } else { //(text,+,+)
  110. $end = $str_list2[$start + ($length - 1)] - $str_list[$start] +1;
  111. return substr($string,$str_list[$start],$end);
  112. }
  113. } else { // (text ,-,+)
  114. $start = $ptr + $start;
  115. if (($start + $length) >= count($str_list2)) {
  116. return substr($string,$str_list[$start]);
  117. } else {
  118. $end = $str_list2[$start + ($length - 1)] - $str_list[$start] +1;
  119. return substr($string,$str_list[$start],$end);
  120. }
  121. }
  122. } else { // $length < 0
  123. $end = strlen($string) - $str_list[$ptr+$length];
  124. if ($start >= 0) {// (text,+,-) {
  125. return substr($string,$str_list[$start],-$end);
  126. } else { //(text,-,-)
  127. $start = $ptr + $start;
  128. return substr($string,$str_list[$start],-$end);
  129. }
  130. } // END OF LENGTH > / < 0
  131. }
  132. } // END IF
  133. }
  134. function GB_replace($needle,$string,$haystack) {
  135. $l = strlen($haystack);
  136. $l2 = strlen($needle);
  137. $l3 = strlen($string);
  138. $news = "";
  139. $skip = 0;
  140. $a = 0;
  141. while ($a < $l) {
  142. $ch = substr($haystack,$a,1);
  143. $ch2 = substr($haystack,$a+1,1);
  144. if (ord($ch) >= HexDec("0x81") && ord($ch2) >= HexDec("0x40")) {
  145. if (substr($haystack,$a,$l2) == $needle) {
  146. $news .= $string;
  147. $a += $l2;
  148. } else {
  149. $news .= $ch.$ch2;
  150. $a += 2;
  151. }
  152. } else {
  153. if (substr($haystack,$a,$l2) == $needle) {
  154. $news .= $string;
  155. $a += $l2;
  156. } else {
  157. $news .= $ch;
  158. $a++;
  159. }
  160. } // END IF
  161. } // END WHILE
  162. return $news;
  163. }
  164. function GB_replace_i($needle,$str_f,$str_b,$haystack) {
  165. $l = strlen($haystack);
  166. $l2 = strlen($needle);
  167. $l3 = strlen($string);
  168. $news = "";
  169. $skip = 0;
  170. $a = 0;
  171. while ($a < $l) {
  172. $ch = substr($haystack,$a,1);
  173. $ch2 = substr($haystack,$a+1,1);
  174. if (ord($ch) >= HexDec("0x81") && ord($ch2) >= HexDec("0x40")) {
  175. if (GBcase(substr($haystack,$a,$l2),"lower") == GBcase($needle,"lower")) {
  176. $news .= $str_f . substr($haystack,$a,$l2) . $str_b;
  177. $a += $l2;
  178. } else {
  179. $news .= $ch.$ch2;
  180. $a += 2;
  181. }
  182. } else {
  183. if (GBcase(substr($haystack,$a,$l2),"lower") == GBcase($needle,"lower")) {
  184. $news .= $str_f . substr($haystack,$a,$l2) . $str_b;
  185. $a += $l2;
  186. } else {
  187. $news .= $ch;
  188. $a++;
  189. }
  190. } // END IF
  191. } // END WHILE
  192. return $news;
  193. }
  194. function GBpos($haystack,$needle,$offset) {
  195. if (!is_int($offset)) {
  196. $offset = strtolower($offset);
  197. if ($offset != "" && $offset != "r" && $offset != "a") {
  198. return "错误:offset 值错误。<br>";
  199. }
  200. }
  201. $l = strlen($haystack);
  202. $l2 = strlen($needle);
  203. $found = false;
  204. $w = 0; // WORD
  205. $a = 0; // START
  206. if ($offset == "" || $offset == "r") {
  207. $atleast = 0;
  208. $value = false;
  209. } elseif ($offset == "a") {
  210. $value = array();
  211. $atleast = 0;
  212. } else {
  213. $value = false;
  214. $atleast = $offset;
  215. }
  216. while ($a < $l) {
  217. $ch = substr($haystack,$a,1);
  218. $ch2 = substr($haystack,$a+1,1);
  219. if (ord($ch) >= HexDec("0x81") && ord($ch2) >= HexDec("0x40") && $skip == 0) {
  220. if (substr($haystack,$a,$l2) == $needle) {
  221. if ($offset == "r") {
  222. $found = true;
  223. $value = $w;
  224. } elseif ($offset == "a") {
  225. $found = true;
  226. $value[] = $w;
  227. } elseif (!$value) {
  228. if ($w >= $atleast) {
  229. $found = true;
  230. $value = $w;
  231. }
  232. }
  233. }
  234. $a += 2;
  235. } else {
  236. if (substr($haystack,$a,$l2) == $needle) {
  237. if ($offset == "r") {
  238. $found = true;
  239. $value = $w;
  240. } elseif ($offset == "a") {
  241. $found = true;
  242. $value[] = $w;
  243. } elseif (!$value) {
  244. if ($w >= $atleast) {
  245. $found = true;
  246. $value = $w;
  247. }
  248. }
  249. }
  250. $a++;
  251. }
  252. $w++;
  253. } // END OF WHILE
  254. if ($found) {
  255. return $value;
  256. } else {
  257. return $false;
  258. }
  259. //} // END OF WHILE
  260. }
  261. function GBrev($text) {
  262. $news = "";
  263. $l = strlen($text);
  264. $GB = 0;
  265. $a = 0;
  266. while ($a < $l) {
  267. $ch = substr($text,$a,1);
  268. $ch2 = substr($text,$a+1,1);
  269. if (ord($ch) >= HexDec("0x81") && ord($ch2) >= HexDec("0x40") && $skip == 0) {
  270. $a += 2;
  271. $news = $ch . $ch2 . $news;
  272. } else {
  273. $news = $ch . $news;
  274. $a++;
  275. }
  276. }
  277. return $news;
  278. }
  279. function GB_check($text) {
  280. $l = strlen($text);
  281. $a = 0;
  282. while ($a < $l) {
  283. $ch = substr($text,$a,1);
  284. $ch2 = substr($text,$a+1,1);
  285. if (ord($ch) >= HexDec("0x81") && ord($ch2) >= HexDec("0x40")) {
  286. return true;
  287. } else {
  288. return false;
  289. }
  290. }
  291. }
  292. function GB_all ($text) {
  293. $l = strlen($text);
  294. $all = 1;
  295. $a = 0;
  296. while ($a < $l) {
  297. $ch = substr($text,$a,1);
  298. $ch2 = substr($text,$a+1,1);
  299. if (ord($ch) >= HexDec("0x81") && ord($ch2) >= HexDec("0x40")) {
  300. $a += 2;
  301. } else {
  302. $a++;
  303. $all = 0;
  304. }
  305. }
  306. if ($all == 1) {
  307. return true;
  308. } else {
  309. return false;
  310. }
  311. }
  312. function GB_non ($text) {
  313. $l = strlen($text);
  314. $all = 1;
  315. $a = 0;
  316. while ($a < $l) {
  317. $ch = substr($text,$a,1);
  318. $ch2 = substr($text,$a+1,1);
  319. if (ord($ch) >= HexDec("0x81") && ord($ch2) >= HexDec("0x40")) {
  320. $a += 2;
  321. $all = 0;
  322. } else {
  323. $a++;
  324. }
  325. }
  326. if ($all == 1) {
  327. return true;
  328. } else {
  329. return false;
  330. }
  331. }
  332. function GBcase ($text,$case) {
  333. $case = strtolower($case);
  334. if ($case != "upper" && $case != "lower" && $case != "ucwords" && $case != "ucfirst") {
  335. return "函数用法错误。 $case";
  336. } else {
  337. $ucfirst = 0;
  338. $ucwords = 0;
  339. $news = "";
  340. $l = strlen($text);
  341. $GB = 0;
  342. $english = 0;
  343. $a = 0;
  344. while ($a < $l) {
  345. $ch = substr($text,$a,1);
  346. if ($GB == 0 && ord($ch) >= HexDec("0x81")) {
  347. $GB = 1;
  348. $english = 0;
  349. $news .= $ch;
  350. $ucwords = 0;
  351. } elseif ($GB == 1 && ord($ch) >= HexDec("0x40") && $english == 0) {
  352. $news .= "$ch";
  353. $ucwords = 0;
  354. $GB = 0;
  355. } else {
  356. if ($case == "upper") {
  357. $news .= strtoupper($ch);
  358. } elseif ($case == "lower") {
  359. $news .= strtolower($ch);
  360. } elseif ($case == "ucwords") {
  361. if ($ucwords == 0) {
  362. $news .= strtoupper($ch);
  363. } else {
  364. $news .= strtolower($ch);
  365. }
  366. $ucwords = 1;
  367. } elseif ($case == "ucfirst") {
  368. if ($ucfirst == 0) {
  369. $news .= strtoupper($ch);
  370. $ucfirst = 1;
  371. } else {
  372. $news .= strtolower($ch);
  373. $ucfirst = 1;
  374. }
  375. } else {
  376. $news .= $ch;
  377. }
  378. if ($ch == " " || $ch == "\n") {
  379. $ucwords = 0;
  380. }
  381. $english = 1;
  382. $GB = 0;
  383. }
  384. $a++;
  385. } // END OF while
  386. return $news;
  387. } // end else
  388. }
  389. function GBspace ($text) {
  390. $news = "";
  391. $l = strlen($text);
  392. $GB = 0;
  393. $english = 0;
  394. $a = 0;
  395. while ($a < $l) {
  396. $ch = substr($text,$a,1);
  397. $ch2 = substr($text,$a+1,1);
  398. if (!($ch == " " && $ch2 == " ")) {
  399. if ($GB == 0) {
  400. if (ord($ch) >= HexDec("0x81")) {
  401. if ($english == 1) {
  402. if ((substr($text,$a-1,1) == " ") || (substr($text,$a-1,1) == "\n")) {
  403. $news .= "$ch";
  404. } else {
  405. $news .= " $ch";
  406. }
  407. $english = 0;
  408. $GB = 1;
  409. } else {
  410. $GB = 1;
  411. $english = 0;
  412. $news .= $ch;
  413. }
  414. } else {
  415. $english = 1;
  416. $GB = 0;
  417. $news .= $ch;
  418. }
  419. } else {
  420. if (ord($ch) >= HexDec("0x40")) {
  421. if ($english == 0) {
  422. if ((substr($text,$a+1,1) == " ")|| (substr($text,$a+1,1) == "\n")) {
  423. $news .= "$ch";
  424. } else {
  425. $news .= "$ch ";
  426. }
  427. } else {
  428. $news .= " $ch";
  429. }
  430. } else {
  431. $english = 1;
  432. $news .= "$ch";
  433. }
  434. $GB = 0;
  435. }
  436. }
  437. $a++;
  438. } // END OF while
  439. // Chk 1 & last is space
  440. $l = strlen($news);
  441. if (substr($news,0,1) == " ") {
  442. $news = substr($news,1);
  443. }
  444. $l = strlen($news);
  445. if (substr($news,$l-1,1) == " ") {
  446. $news = substr($news,0,$l-1);
  447. }
  448. return $news;
  449. }
  450. function GBunspace($text) {
  451. $news = "";
  452. $l = strlen($text);
  453. $a = 0;
  454. $last_space = 1;
  455. while ($a < $l) {
  456. $ch = substr($text,$a,1);
  457. $ch2 = substr($text,$a+1,1);
  458. $ch3 = substr($text,$a+2,1);
  459. if (($a + 1) == $l ) {
  460. $last_space = 1;
  461. }
  462. if ($ch == " ") {
  463. if ($last_space == 0) {
  464. if (ord($ch2) >= HexDec("0x81") && ord($ch3) >= HexDec("0x40")) {
  465. if ($chi == 0) {
  466. $news .= " ";
  467. $last_space = 1;
  468. }
  469. $chi=1;
  470. } elseif ($ch2 != " ") {
  471. $news .= " ";
  472. $chi = 0;
  473. $last_space = 1;
  474. }
  475. }
  476. } else {
  477. if (ord($ch) >= HexDec("0x81") && ord($ch2) >= HexDec("0x40")) {
  478. $chi = 1;
  479. $a++;
  480. $news .= $ch . $ch2;
  481. $last_space = 0;
  482. } else {
  483. $chi = 0;
  484. $news .= $ch;
  485. $last_space = 0;
  486. }
  487. }
  488. $a++;
  489. }
  490. // Chk 1 & last is space
  491. $l = strlen($news);
  492. if (substr($news,0,1) == " ") {
  493. $news = substr($news,1);
  494. }
  495. $l = strlen($news);
  496. if (substr($news,$l-1,1) == " ") {
  497. $news = substr($news,0,$l-1);
  498. }
  499. return $news;
  500. } // END OF Function
  501. function GBstrnear($text,$length) {
  502. $tex_len = strlen($text);
  503. $a = 0;
  504. $w = "";
  505. while ($a < $tex_len) {
  506. $ch = substr($text,$a,1);
  507. $ch2 = substr($text,$a+1,1);
  508. if (GB_all($ch.$ch2)) {
  509. $w .= $ch.$ch2;
  510. $a=$a+2;
  511. } else {
  512. $w .= $ch;
  513. $a++;
  514. }
  515. if ($a == $length || $a == ($length - 1)) {
  516. $a = $tex_len;
  517. }
  518. }
  519. return $w;
  520. } // END OF FUNCTION
  521. function clear_space($text) {
  522. $t = "";
  523. for ($a=0;$a<strlen($text);$a++) {
  524. $ch = substr($text,$a,1);
  525. $ch2 = substr($text,$a+1,1);
  526. if ($ch == " " && $ch2 == " ") {
  527. } else {
  528. $t .= $ch;
  529. }
  530. }
  531. return $t;
  532. }
  533. ?>