Home
Backend Development
PHP Tutorial
PHP implementation code to obtain the first letter of Chinese characters (gb2312 encoding)



PHP implementation code to obtain the first letter of Chinese characters (gb2312 encoding)
Jul 25, 2016 am 08:57 AM
This article introduces how to use PHP to obtain the first letter of a Chinese character. The encoding format is gb2312. Friends in need can refer to it.
php gets the first letter of Chinese characters, code: <?php //取GB2312字符串首字母 //GBK汉字是按拼音顺序编码的 function get_letter($input){ $dict=array( 'a'=>0xB0C4, 'b'=>0xB2C0, 'c'=>0xB4ED, 'd'=>0xB6E9, 'e'=>0xB7A1, 'f'=>0xB8C0, 'g'=>0xB9FD, 'h'=>0xBBF6, 'j'=>0xBFA5, 'k'=>0xC0AB, 'l'=>0xC2E7, 'm'=>0xC4C2, 'n'=>0xC5B5, 'o'=>0xC5BD, 'p'=>0xC6D9, 'q'=>0xC8BA, 'r'=>0xC8F5, 's'=>0xCBF9, 't'=>0xCDD9, 'w'=>0xCEF3, 'x'=>0xD188, 'y'=>0xD4D0, 'z'=>0xD7F9, ); $str_1 = substr($input, 0, 1); if ($str_1 >= chr(0x81) && $str_1 <= chr(0xfe)) { $num = hexdec(bin2hex(substr($input, 0, 2))); foreach ($dict as $k=>$v){ if($v>=$num) break; } return $k; } else{ return $str_1; } //by bbs.it-home.org } echo get_letter('脚'); echo get_letter('本'); echo get_letter('学'); echo get_letter('堂'); echo get_letter('欢'); echo get_letter('迎'); echo get_letter('您'); echo get_letter('网站编程'); ?> Copy after login This is a pretty good piece of code, especially suitable for taking the first letter in the title and generating a list of articles classified by letters. |
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

Hot Article
How Long Does It Take To Beat Split Fiction?
3 weeks ago
By DDD
Repo: How To Revive Teammates
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌

Hot tools Tags

Hot Article
How Long Does It Take To Beat Split Fiction?
3 weeks ago
By DDD
Repo: How To Revive Teammates
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

11 Best PHP URL Shortener Scripts (Free and Premium)

Working with Flash Session Data in Laravel

Simplified HTTP Response Mocking in Laravel Tests

Build a React App With a Laravel Back End: Part 2, React

cURL in PHP: How to Use the PHP cURL Extension in REST APIs

12 Best PHP Chat Scripts on CodeCanyon
