PHP does not use the iconv library to perform gb2312 and utf-8 encoding conversion functions
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-25 09:08:06
Original
1039 people have browsed it
-
-
//Usage of comparison table - $filename = "gb2utf8.txt";
- $fp = fopen($filename,"r");
- while(! feof ($fp)) {
- list($gb,$utf8) = fgetcsv($fp,10);
- $charset[$gb] = $utf8;
- }
- fclose($fp);
- //The above reading Lookup table to array for later use
/**gb2312 to utf-8**/
- function gb2utf8($text, &$charset) {
- //Extract the components in the text, Chinese characters are one element, continuous The non-Chinese characters are an element
- preg_match_all("/(?:[x80-xff].)|[x01-x7f]+/",$text,$tmp);
- $tmp = $tmp[0];
- / /Separate Chinese characters
- $ar = array_intersect($tmp, array_keys($charset));
- //Replace Chinese character encoding
- foreach($ar as $k=>$v)
- $tmp[$k] = $charset [$v];
- //Return the escaped string
- return join('',$tmp);
- }
/**utf-8 to gb2312**/
- function utf82gb($ text, &$charset) {
- $p = "/[xf0-xf7][x80-xbf]{3}|[xe0-xef][x80-xbf]{2}|[xc2-xdf][x80-xbf ]|[x01-x7f]+/";
- preg_match_all($p,$text,$r);
- $utf8 = array_flip($charset);
- foreach($r[0] as $k=>$v )
- if(isset($utf8[$v]))
- $r[0][$k] = $utf8[$v];
- return join('',$r[0]);
- }< /p>
//Test
- $s = gb2utf8('This is a test of the comparison table', $charset);
- echo utf82gb($s, $charset);
- ?>
-
Copy code
|
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
-
2025-02-26 03:58:14
-
2025-02-26 03:38:10
-
2025-02-26 03:17:10
-
2025-02-26 02:49:09
-
2025-02-26 01:08:13
-
2025-02-26 00:46:10
-
2025-02-25 23:42:08
-
2025-02-25 22:50:13
-
2025-02-25 21:54:11
-
2025-02-25 20:45:11