PageRenderTime 53ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/functions/extra_functions/functions_chn.php

https://github.com/happyxlq/zencart_svn
PHP | 613 lines | 489 code | 59 blank | 65 comment | 207 complexity | 4f6cd05b36ba6b99f45ee2916be28043 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. // A trim function to remove the last character of a utf-8 string
  54. // by following instructions on http://en.wikipedia.org/wiki/UTF-8
  55. // dotann
  56. // usage: $str = utf8_trim(substr($str,0,50));
  57. function utf8_trim($str) {
  58. $len = strlen($str);
  59. for ($i=strlen($str)-1; $i>=0; $i-=1){
  60. $hex .= ' '.ord($str[$i]);
  61. $ch = ord($str[$i]);
  62. if (($ch & 128)==0) return(substr($str,0,$i));
  63. if (($ch & 192)==192) return(substr($str,0,$i));
  64. }
  65. return($str.$hex);
  66. }
  67. function GBlen($string) {
  68. $l = strlen($string);
  69. $ptr = 0;
  70. $a = 0;
  71. while ($a < $l) {
  72. $ch = substr($string,$a,1);
  73. $ch2 = substr($string,$a+1,1);
  74. if (ord($ch) >= HexDec("0x81") && ord($ch2) >= HexDec("0x40")) {
  75. $ptr++;
  76. $a += 2;
  77. } else {
  78. $ptr++;
  79. $a++;
  80. } // END IF
  81. } // END WHILE
  82. return $ptr;
  83. }
  84. function GBsubstr($string,$start,$length) {
  85. if (!is_int($length) && $length != "") {
  86. return "错误:length 值错误(必须为数值)。<br>";
  87. } elseif ($length == "0") {
  88. return "";
  89. } else {
  90. $l = strlen($string);
  91. $a = 0;
  92. $ptr = 0;
  93. $str_list = array();
  94. $str_list2 = array();
  95. while ($a < $l) {
  96. $ch = substr($string,$a,1);
  97. $ch2 = substr($string,$a+1,1);
  98. if (ord($ch) >= HexDec("0x81") && ord($ch2) >= HexDec("0x40")) {
  99. $str_list[$ptr] = $a;
  100. $str_list2[$ptr] = $a+1;
  101. $ptr++;
  102. $a += 2;
  103. } else {
  104. $str_list[$ptr] = $a;
  105. $str_list2[$ptr] = $a;
  106. $ptr++;
  107. $a++;
  108. } // END IF
  109. } // END WHILE
  110. if ($start > $ptr || -$start > $ptr) {
  111. return;
  112. } elseif ($length == "") {
  113. if ($start >= 0) { // (text,+)
  114. return substr($string,$str_list[$start]);
  115. } else { // (test,-)
  116. return substr($string,$str_list[$ptr + $start]);
  117. }
  118. } else {
  119. if ($length > 0) { // $length > 0
  120. if ($start >= 0) {// (text,+,+)
  121. if (($start + $length) >= count($str_list2)) {
  122. return substr($string,$str_list[$start]);
  123. } else { //(text,+,+)
  124. $end = $str_list2[$start + ($length - 1)] - $str_list[$start] +1;
  125. return substr($string,$str_list[$start],$end);
  126. }
  127. } else { // (text ,-,+)
  128. $start = $ptr + $start;
  129. if (($start + $length) >= count($str_list2)) {
  130. return substr($string,$str_list[$start]);
  131. } else {
  132. $end = $str_list2[$start + ($length - 1)] - $str_list[$start] +1;
  133. return substr($string,$str_list[$start],$end);
  134. }
  135. }
  136. } else { // $length < 0
  137. $end = strlen($string) - $str_list[$ptr+$length];
  138. if ($start >= 0) {// (text,+,-) {
  139. return substr($string,$str_list[$start],-$end);
  140. } else { //(text,-,-)
  141. $start = $ptr + $start;
  142. return substr($string,$str_list[$start],-$end);
  143. }
  144. } // END OF LENGTH > / < 0
  145. }
  146. } // END IF
  147. }
  148. function GB_replace($needle,$string,$haystack) {
  149. $l = strlen($haystack);
  150. $l2 = strlen($needle);
  151. $l3 = strlen($string);
  152. $news = "";
  153. $skip = 0;
  154. $a = 0;
  155. while ($a < $l) {
  156. $ch = substr($haystack,$a,1);
  157. $ch2 = substr($haystack,$a+1,1);
  158. if (ord($ch) >= HexDec("0x81") && ord($ch2) >= HexDec("0x40")) {
  159. if (substr($haystack,$a,$l2) == $needle) {
  160. $news .= $string;
  161. $a += $l2;
  162. } else {
  163. $news .= $ch.$ch2;
  164. $a += 2;
  165. }
  166. } else {
  167. if (substr($haystack,$a,$l2) == $needle) {
  168. $news .= $string;
  169. $a += $l2;
  170. } else {
  171. $news .= $ch;
  172. $a++;
  173. }
  174. } // END IF
  175. } // END WHILE
  176. return $news;
  177. }
  178. function GB_replace_i($needle,$str_f,$str_b,$haystack) {
  179. $l = strlen($haystack);
  180. $l2 = strlen($needle);
  181. $l3 = strlen($string);
  182. $news = "";
  183. $skip = 0;
  184. $a = 0;
  185. while ($a < $l) {
  186. $ch = substr($haystack,$a,1);
  187. $ch2 = substr($haystack,$a+1,1);
  188. if (ord($ch) >= HexDec("0x81") && ord($ch2) >= HexDec("0x40")) {
  189. if (GBcase(substr($haystack,$a,$l2),"lower") == GBcase($needle,"lower")) {
  190. $news .= $str_f . substr($haystack,$a,$l2) . $str_b;
  191. $a += $l2;
  192. } else {
  193. $news .= $ch.$ch2;
  194. $a += 2;
  195. }
  196. } else {
  197. if (GBcase(substr($haystack,$a,$l2),"lower") == GBcase($needle,"lower")) {
  198. $news .= $str_f . substr($haystack,$a,$l2) . $str_b;
  199. $a += $l2;
  200. } else {
  201. $news .= $ch;
  202. $a++;
  203. }
  204. } // END IF
  205. } // END WHILE
  206. return $news;
  207. }
  208. function GBpos($haystack,$needle,$offset) {
  209. if (!is_int($offset)) {
  210. $offset = strtolower($offset);
  211. if ($offset != "" && $offset != "r" && $offset != "a") {
  212. return "错误:offset 值错误。<br>";
  213. }
  214. }
  215. $l = strlen($haystack);
  216. $l2 = strlen($needle);
  217. $found = false;
  218. $w = 0; // WORD
  219. $a = 0; // START
  220. if ($offset == "" || $offset == "r") {
  221. $atleast = 0;
  222. $value = false;
  223. } elseif ($offset == "a") {
  224. $value = array();
  225. $atleast = 0;
  226. } else {
  227. $value = false;
  228. $atleast = $offset;
  229. }
  230. while ($a < $l) {
  231. $ch = substr($haystack,$a,1);
  232. $ch2 = substr($haystack,$a+1,1);
  233. if (ord($ch) >= HexDec("0x81") && ord($ch2) >= HexDec("0x40") && $skip == 0) {
  234. if (substr($haystack,$a,$l2) == $needle) {
  235. if ($offset == "r") {
  236. $found = true;
  237. $value = $w;
  238. } elseif ($offset == "a") {
  239. $found = true;
  240. $value[] = $w;
  241. } elseif (!$value) {
  242. if ($w >= $atleast) {
  243. $found = true;
  244. $value = $w;
  245. }
  246. }
  247. }
  248. $a += 2;
  249. } else {
  250. if (substr($haystack,$a,$l2) == $needle) {
  251. if ($offset == "r") {
  252. $found = true;
  253. $value = $w;
  254. } elseif ($offset == "a") {
  255. $found = true;
  256. $value[] = $w;
  257. } elseif (!$value) {
  258. if ($w >= $atleast) {
  259. $found = true;
  260. $value = $w;
  261. }
  262. }
  263. }
  264. $a++;
  265. }
  266. $w++;
  267. } // END OF WHILE
  268. if ($found) {
  269. return $value;
  270. } else {
  271. return $false;
  272. }
  273. //} // END OF WHILE
  274. }
  275. function GBrev($text) {
  276. $news = "";
  277. $l = strlen($text);
  278. $GB = 0;
  279. $a = 0;
  280. while ($a < $l) {
  281. $ch = substr($text,$a,1);
  282. $ch2 = substr($text,$a+1,1);
  283. if (ord($ch) >= HexDec("0x81") && ord($ch2) >= HexDec("0x40") && $skip == 0) {
  284. $a += 2;
  285. $news = $ch . $ch2 . $news;
  286. } else {
  287. $news = $ch . $news;
  288. $a++;
  289. }
  290. }
  291. return $news;
  292. }
  293. function GB_check($text) {
  294. $l = strlen($text);
  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. return true;
  301. } else {
  302. return false;
  303. }
  304. }
  305. }
  306. function GB_all ($text) {
  307. $l = strlen($text);
  308. $all = 1;
  309. $a = 0;
  310. while ($a < $l) {
  311. $ch = substr($text,$a,1);
  312. $ch2 = substr($text,$a+1,1);
  313. if (ord($ch) >= HexDec("0x81") && ord($ch2) >= HexDec("0x40")) {
  314. $a += 2;
  315. } else {
  316. $a++;
  317. $all = 0;
  318. }
  319. }
  320. if ($all == 1) {
  321. return true;
  322. } else {
  323. return false;
  324. }
  325. }
  326. function GB_non ($text) {
  327. $l = strlen($text);
  328. $all = 1;
  329. $a = 0;
  330. while ($a < $l) {
  331. $ch = substr($text,$a,1);
  332. $ch2 = substr($text,$a+1,1);
  333. if (ord($ch) >= HexDec("0x81") && ord($ch2) >= HexDec("0x40")) {
  334. $a += 2;
  335. $all = 0;
  336. } else {
  337. $a++;
  338. }
  339. }
  340. if ($all == 1) {
  341. return true;
  342. } else {
  343. return false;
  344. }
  345. }
  346. function GBcase ($text,$case) {
  347. $case = strtolower($case);
  348. if ($case != "upper" && $case != "lower" && $case != "ucwords" && $case != "ucfirst") {
  349. return "函数用法错误。 $case";
  350. } else {
  351. $ucfirst = 0;
  352. $ucwords = 0;
  353. $news = "";
  354. $l = strlen($text);
  355. $GB = 0;
  356. $english = 0;
  357. $a = 0;
  358. while ($a < $l) {
  359. $ch = substr($text,$a,1);
  360. if ($GB == 0 && ord($ch) >= HexDec("0x81")) {
  361. $GB = 1;
  362. $english = 0;
  363. $news .= $ch;
  364. $ucwords = 0;
  365. } elseif ($GB == 1 && ord($ch) >= HexDec("0x40") && $english == 0) {
  366. $news .= "$ch";
  367. $ucwords = 0;
  368. $GB = 0;
  369. } else {
  370. if ($case == "upper") {
  371. $news .= strtoupper($ch);
  372. } elseif ($case == "lower") {
  373. $news .= strtolower($ch);
  374. } elseif ($case == "ucwords") {
  375. if ($ucwords == 0) {
  376. $news .= strtoupper($ch);
  377. } else {
  378. $news .= strtolower($ch);
  379. }
  380. $ucwords = 1;
  381. } elseif ($case == "ucfirst") {
  382. if ($ucfirst == 0) {
  383. $news .= strtoupper($ch);
  384. $ucfirst = 1;
  385. } else {
  386. $news .= strtolower($ch);
  387. $ucfirst = 1;
  388. }
  389. } else {
  390. $news .= $ch;
  391. }
  392. if ($ch == " " || $ch == "\n") {
  393. $ucwords = 0;
  394. }
  395. $english = 1;
  396. $GB = 0;
  397. }
  398. $a++;
  399. } // END OF while
  400. return $news;
  401. } // end else
  402. }
  403. function GBspace ($text) {
  404. $news = "";
  405. $l = strlen($text);
  406. $GB = 0;
  407. $english = 0;
  408. $a = 0;
  409. while ($a < $l) {
  410. $ch = substr($text,$a,1);
  411. $ch2 = substr($text,$a+1,1);
  412. if (!($ch == " " && $ch2 == " ")) {
  413. if ($GB == 0) {
  414. if (ord($ch) >= HexDec("0x81")) {
  415. if ($english == 1) {
  416. if ((substr($text,$a-1,1) == " ") || (substr($text,$a-1,1) == "\n")) {
  417. $news .= "$ch";
  418. } else {
  419. $news .= " $ch";
  420. }
  421. $english = 0;
  422. $GB = 1;
  423. } else {
  424. $GB = 1;
  425. $english = 0;
  426. $news .= $ch;
  427. }
  428. } else {
  429. $english = 1;
  430. $GB = 0;
  431. $news .= $ch;
  432. }
  433. } else {
  434. if (ord($ch) >= HexDec("0x40")) {
  435. if ($english == 0) {
  436. if ((substr($text,$a+1,1) == " ")|| (substr($text,$a+1,1) == "\n")) {
  437. $news .= "$ch";
  438. } else {
  439. $news .= "$ch ";
  440. }
  441. } else {
  442. $news .= " $ch";
  443. }
  444. } else {
  445. $english = 1;
  446. $news .= "$ch";
  447. }
  448. $GB = 0;
  449. }
  450. }
  451. $a++;
  452. } // END OF while
  453. // Chk 1 & last is space
  454. $l = strlen($news);
  455. if (substr($news,0,1) == " ") {
  456. $news = substr($news,1);
  457. }
  458. $l = strlen($news);
  459. if (substr($news,$l-1,1) == " ") {
  460. $news = substr($news,0,$l-1);
  461. }
  462. return $news;
  463. }
  464. function GBunspace($text) {
  465. $news = "";
  466. $l = strlen($text);
  467. $a = 0;
  468. $last_space = 1;
  469. while ($a < $l) {
  470. $ch = substr($text,$a,1);
  471. $ch2 = substr($text,$a+1,1);
  472. $ch3 = substr($text,$a+2,1);
  473. if (($a + 1) == $l ) {
  474. $last_space = 1;
  475. }
  476. if ($ch == " ") {
  477. if ($last_space == 0) {
  478. if (ord($ch2) >= HexDec("0x81") && ord($ch3) >= HexDec("0x40")) {
  479. if ($chi == 0) {
  480. $news .= " ";
  481. $last_space = 1;
  482. }
  483. $chi=1;
  484. } elseif ($ch2 != " ") {
  485. $news .= " ";
  486. $chi = 0;
  487. $last_space = 1;
  488. }
  489. }
  490. } else {
  491. if (ord($ch) >= HexDec("0x81") && ord($ch2) >= HexDec("0x40")) {
  492. $chi = 1;
  493. $a++;
  494. $news .= $ch . $ch2;
  495. $last_space = 0;
  496. } else {
  497. $chi = 0;
  498. $news .= $ch;
  499. $last_space = 0;
  500. }
  501. }
  502. $a++;
  503. }
  504. // Chk 1 & last is space
  505. $l = strlen($news);
  506. if (substr($news,0,1) == " ") {
  507. $news = substr($news,1);
  508. }
  509. $l = strlen($news);
  510. if (substr($news,$l-1,1) == " ") {
  511. $news = substr($news,0,$l-1);
  512. }
  513. return $news;
  514. } // END OF Function
  515. function GBstrnear($text,$length) {
  516. $tex_len = strlen($text);
  517. $a = 0;
  518. $w = "";
  519. while ($a < $tex_len) {
  520. $ch = substr($text,$a,1);
  521. $ch2 = substr($text,$a+1,1);
  522. if (GB_all($ch.$ch2)) {
  523. $w .= $ch.$ch2;
  524. $a=$a+2;
  525. } else {
  526. $w .= $ch;
  527. $a++;
  528. }
  529. if ($a == $length || $a == ($length - 1)) {
  530. $a = $tex_len;
  531. }
  532. }
  533. return $w;
  534. } // END OF FUNCTION
  535. function clear_space($text) {
  536. $t = "";
  537. for ($a=0;$a<strlen($text);$a++) {
  538. $ch = substr($text,$a,1);
  539. $ch2 = substr($text,$a+1,1);
  540. if ($ch == " " && $ch2 == " ") {
  541. } else {
  542. $t .= $ch;
  543. }
  544. }
  545. return $t;
  546. }
  547. ?>