Home Backend Development PHP Tutorial php-Chinese pinyin initial letter tool class

php-Chinese pinyin initial letter tool class

Aug 08, 2016 am 09:32 AM
gt return str string

<?<span>php


</span><span>/*</span><span>*
* 汉字拼音首字母工具类
*  注: 英文的字串:不变返回(包括数字)    eg .abc123 => abc123
*      中文字符串:返回拼音首字符        eg. 测试字符串 => CSZFC
*      中英混合串: 返回拼音首字符和英文   eg. 我i我j => WIWJ
*  eg.
*  $py = new str2PY();
*  
*  $result = $py->getInitials('周杰伦');
*
*  //获取首字母
*  $result = $py->getFirstString('abc');  //A
*  $resutl = $py->getFirstString("周杰伦"); //Z
*
<span>*/</span>

<span>class</span><span> str2py
{

    </span><span>private</span> <span>$_pinyins</span> = <span>array</span><span>(
        </span>176161 => 'A',
        176197 => 'B',
        178193 => 'C',
        180238 => 'D',
        182234 => 'E',
        183162 => 'F',
        184193 => 'G',
        185254 => 'H',
        187247 => 'J',
        191166 => 'K',
        192172 => 'L',
        194232 => 'M',
        196195 => 'N',
        197182 => 'O',
        197190 => 'P',
        198218 => 'Q',
        200187 => 'R',
        200246 => 'S',
        203250 => 'T',
        205218 => 'W',
        206244 => 'X',
        209185 => 'Y',
        212209 => 'Z',<span>
    );
    </span><span>private</span> <span>$_charset</span> = <span>null</span><span>;

    </span><span>/*</span><span>*
     * 构造函数, 指定需要的编码 default: utf-8
     * 支持utf-8, gb2312
     *
     * @param unknown_type $charset
     </span><span>*/</span>
    <span>public</span> <span>function</span> __construct(<span>$charset</span> = 'utf-8'<span>)
    {
        </span><span>$this</span>->_charset = <span>$charset</span><span>;
    }

    </span><span>/*</span><span>*
     * 中文字符串 substr
     *
     * @param string $str
     * @param int    $start
     * @param int    $len
     * @return string
     </span><span>*/</span>
    <span>private</span> <span>function</span> _msubstr(<span>$str</span>, <span>$start</span>, <span>$len</span><span>)
    {
        </span><span>$start</span> = <span>$start</span> * 2<span>;
        </span><span>$len</span> = <span>$len</span> * 2<span>;
        </span><span>$strlen</span> = <span>strlen</span>(<span>$str</span><span>);
        </span><span>$result</span> = ''<span>;
        </span><span>for</span> (<span>$i</span> = 0; <span>$i</span> < <span>$strlen</span>; <span>$i</span>++<span>)
        {
            </span><span>if</span> (<span>$i</span> >= <span>$start</span> && <span>$i</span> < (<span>$start</span> + <span>$len</span><span>))
            {
                </span><span>if</span> (<span>ord</span>(<span>substr</span>(<span>$str</span>, <span>$i</span>, 1)) > 129<span>)
                {
                    </span><span>$result</span> .= <span>substr</span>(<span>$str</span>, <span>$i</span>, 2<span>);
                }
                </span><span>else</span><span>
                {
                    </span><span>$result</span> .= <span>substr</span>(<span>$str</span>, <span>$i</span>, 1<span>);
                }
            }
            </span><span>if</span> (<span>ord</span>(<span>substr</span>(<span>$str</span>, <span>$i</span>, 1)) > 129<span>)
            {
                </span><span>$i</span>++<span>;
            }
        }
        </span><span>return</span> <span>$result</span><span>;
    }

    </span><span>/*</span><span>*
     * 字符串切分为数组 (汉字或者一个字符为单位)
     *
     * @param string $str
     * @return array
     </span><span>*/</span>
    <span>private</span> <span>function</span> _cutWord(<span>$str</span><span>)
    {
        </span><span>$words</span> = <span>array</span><span>();
        </span><span>while</span> (<span>$str</span> != ""<span>)
        {
            </span><span>if</span> (<span>$this</span>->_isAscii(<span>$str</span><span>))
            {</span><span>/*</span><span> 非中文 </span><span>*/</span>
                <span>$words</span>[] = <span>$str</span>[0<span>];
                </span><span>$str</span> = <span>substr</span>(<span>$str</span>, <span>strlen</span>(<span>$str</span>[0<span>]));
            }
            </span><span>else</span><span>
            {
                </span><span>$word</span> = <span>$this</span>->_msubstr(<span>$str</span>, 0, 1<span>);
                </span><span>$words</span>[] = <span>$word</span><span>;
                </span><span>$str</span> = <span>substr</span>(<span>$str</span>, <span>strlen</span>(<span>$word</span><span>));
            }
        }
        </span><span>return</span> <span>$words</span><span>;
    }

    </span><span>/*</span><span>*
     * 判断字符是否是ascii字符
     *
     * @param string $char
     * @return bool
     </span><span>*/</span>
    <span>private</span> <span>function</span> _isAscii(<span>$char</span><span>)
    {
        </span><span>return</span> ( <span>ord</span>(<span>substr</span>(<span>$char</span>, 0, 1)) < 160<span> );
    }

    </span><span>/*</span><span>*
     * 判断字符串前3个字符是否是ascii字符
     *
     * @param string $str
     * @return bool
     </span><span>*/</span>
    <span>private</span> <span>function</span> _isAsciis(<span>$str</span><span>)
    {
        </span><span>$len</span> = <span>strlen</span>(<span>$str</span>) >= 3 ? 3 : 2<span>;
        </span><span>$chars</span> = <span>array</span><span>();
        </span><span>for</span> (<span>$i</span> = 1; <span>$i</span> < <span>$len</span> - 1; <span>$i</span>++<span>)
        {
            </span><span>$chars</span>[] = <span>$this</span>->_isAscii(<span>$str</span>[<span>$i</span>]) ? 'yes' : 'no'<span>;
        }
        </span><span>$result</span> = <span>array_count_values</span>(<span>$chars</span><span>);
        </span><span>if</span> (<span>empty</span>(<span>$result</span>['no'<span>]))
        {
            </span><span>return</span> <span>true</span><span>;
        }
        </span><span>return</span> <span>false</span><span>;
    }

    </span><span>/*</span><span>*
     * 获取中文字串的拼音首字符
     *
     * @param string $str
     * @return string
     </span><span>*/</span>
    <span>public</span> <span>function</span> getInitials(<span>$str</span><span>)
    {
        </span><span>if</span> (<span>empty</span>(<span>$str</span><span>))
            </span><span>return</span> ''<span>;
        </span><span>if</span> (<span>$this</span>->_isAscii(<span>$str</span>[0]) && <span>$this</span>->_isAsciis(<span>$str</span><span>))
        {
            </span><span>return</span> <span>$str</span><span>;
        }
        </span><span>$result</span> = <span>array</span><span>();
        </span><span>if</span> (<span>$this</span>->_charset == 'utf-8'<span>)
        {
            </span><span>$str</span> = <span>iconv</span>('utf-8', 'gb2312', <span>$str</span><span>);
        }
        </span><span>$words</span> = <span>$this</span>->_cutWord(<span>$str</span><span>);
        </span><span>foreach</span> (<span>$words</span> <span>as</span> <span>$word</span><span>)
        {
            </span><span>if</span> (<span>$this</span>->_isAscii(<span>$word</span><span>))
            {</span><span>/*</span><span> 非中文 </span><span>*/</span>
                <span>$result</span>[] = <span>$word</span><span>;
                </span><span>continue</span><span>;
            }
            </span><span>$code</span> = <span>ord</span>(<span>substr</span>(<span>$word</span>, 0, 1)) * 1000 + <span>ord</span>(<span>substr</span>(<span>$word</span>, 1, 1<span>));
            </span><span>/*</span><span> 获取拼音首字母A--Z </span><span>*/</span>
            <span>if</span> ((<span>$i</span> = <span>$this</span>->_search(<span>$code</span>)) != -1<span>)
            {
                </span><span>$result</span>[] = <span>$this</span>->_pinyins[<span>$i</span><span>];
            }
        }
        </span><span>return</span> <span>strtoupper</span>(<span>implode</span>('', <span>$result</span><span>));
    }

    </span><span>/*</span><span>*
     *  20140624 wangtianbao 获取首字母
     *  @param string $str
     *  @return string
     </span><span>*/</span>
    <span>public</span> <span>function</span> getFirstString(<span>$str</span><span>)
    {
        </span><span>//</span><span>先把中文转换成字母</span>
        <span>$new_string</span> = <span>$this</span>->getInitials(<span>$str</span><span>);

        </span><span>if</span> (<span>empty</span>(<span>$new_string</span><span>))
        {
            </span><span>return</span> ''<span>;
        }
        </span><span>else</span><span>
        {
            </span><span>return</span> <span>strtoupper</span>(<span>substr</span>(<span>$new_string</span>, 0, 1<span>));
        }
    }

    </span><span>private</span> <span>function</span> _getChar(<span>$ascii</span><span>)
    {
        </span><span>if</span> (<span>$ascii</span> >= 48 && <span>$ascii</span> <= 57<span>)
        {
            </span><span>return</span> <span>chr</span>(<span>$ascii</span>);  <span>/*</span><span> 数字 </span><span>*/</span><span>
        }
        </span><span>elseif</span> (<span>$ascii</span> >= 65 && <span>$ascii</span> <= 90<span>)
        {
            </span><span>return</span> <span>chr</span>(<span>$ascii</span>);   <span>/*</span><span> A--Z </span><span>*/</span><span>
        }
        </span><span>elseif</span> (<span>$ascii</span> >= 97 && <span>$ascii</span> <= 122<span>)
        {
            </span><span>return</span> <span>chr</span>(<span>$ascii</span> - 32); <span>/*</span><span> a--z </span><span>*/</span><span>
        }
        </span><span>else</span><span>
        {
            </span><span>return</span> '-'; <span>/*</span><span> 其他 </span><span>*/</span><span>
        }
    }

    </span><span>/*</span><span>*
     * 查找需要的汉字内码(gb2312) 对应的拼音字符( 二分法 )
     *
     * @param int $code
     * @return int
     </span><span>*/</span>
    <span>private</span> <span>function</span> _search(<span>$code</span><span>)
    {
        </span><span>$data</span> = <span>array_keys</span>(<span>$this</span>-><span>_pinyins);
        </span><span>$lower</span> = 0<span>;
        </span><span>$upper</span> = <span>sizeof</span>(<span>$data</span>) - 1<span>;
        </span><span>$middle</span> = (int) <span>round</span>((<span>$lower</span> + <span>$upper</span>) / 2<span>);
        </span><span>if</span> (<span>$code</span> < <span>$data</span>[0<span>])
            </span><span>return</span> -1<span>;
        </span><span>for</span><span> (;;)
        {
            </span><span>if</span> (<span>$lower</span> > <span>$upper</span><span>)
            {
                </span><span>return</span> <span>$data</span>[<span>$lower</span> - 1<span>];
            }
            </span><span>$tmp</span> = (int) <span>round</span>((<span>$lower</span> + <span>$upper</span>) / 2<span>);
            </span><span>if</span> (!<span>isset</span>(<span>$data</span>[<span>$tmp</span><span>]))
            {
                </span><span>return</span> <span>$data</span>[<span>$middle</span><span>];
            }
            </span><span>else</span><span>
            {
                </span><span>$middle</span> = <span>$tmp</span><span>;
            }
            </span><span>if</span> (<span>$data</span>[<span>$middle</span>] < <span>$code</span><span>)
            {
                </span><span>$lower</span> = (int) <span>$middle</span> + 1<span>;
            }
            </span><span>else</span> <span>if</span> (<span>$data</span>[<span>$middle</span>] == <span>$code</span><span>)
            {
                </span><span>return</span> <span>$data</span>[<span>$middle</span><span>];
            }
            </span><span>else</span><span>
            {
                </span><span>$upper</span> = (int) <span>$middle</span> - 1<span>;
            }
        }
    }

}</span>
Copy after login

The above introduces the php-Chinese pinyin initial letter tool class, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

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 AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the differences between Huawei GT3 Pro and GT4? What are the differences between Huawei GT3 Pro and GT4? Dec 29, 2023 pm 02:27 PM

Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

Convert basic data types to strings using Java's String.valueOf() function Convert basic data types to strings using Java's String.valueOf() function Jul 24, 2023 pm 07:55 PM

Convert basic data types to strings using Java's String.valueOf() function In Java development, when we need to convert basic data types to strings, a common method is to use the valueOf() function of the String class. This function can accept parameters of basic data types and return the corresponding string representation. In this article, we will explore how to use the String.valueOf() function for basic data type conversions and provide some code examples to

Detailed explanation of the usage of return in C language Detailed explanation of the usage of return in C language Oct 07, 2023 am 10:58 AM

The usage of return in C language is: 1. For functions whose return value type is void, you can use the return statement to end the execution of the function early; 2. For functions whose return value type is not void, the function of the return statement is to end the execution of the function. The result is returned to the caller; 3. End the execution of the function early. Inside the function, we can use the return statement to end the execution of the function early, even if the function does not return a value.

How to convert char array to string How to convert char array to string Jun 09, 2023 am 10:04 AM

Method of converting char array to string: It can be achieved by assignment. Use {char a[]=" abc d\0efg ";string s=a;} syntax to let the char array directly assign a value to string, and execute the code to complete the conversion.

Fix: Snipping tool not working in Windows 11 Fix: Snipping tool not working in Windows 11 Aug 24, 2023 am 09:48 AM

Why Snipping Tool Not Working on Windows 11 Understanding the root cause of the problem can help find the right solution. Here are the top reasons why the Snipping Tool might not be working properly: Focus Assistant is On: This prevents the Snipping Tool from opening. Corrupted application: If the snipping tool crashes on launch, it might be corrupted. Outdated graphics drivers: Incompatible drivers may interfere with the snipping tool. Interference from other applications: Other running applications may conflict with the Snipping Tool. Certificate has expired: An error during the upgrade process may cause this issu simple solution. These are suitable for most users and do not require any special technical knowledge. 1. Update Windows and Microsoft Store apps

What is the execution order of return and finally statements in Java? What is the execution order of return and finally statements in Java? Apr 25, 2023 pm 07:55 PM

Source code: publicclassReturnFinallyDemo{publicstaticvoidmain(String[]args){System.out.println(case1());}publicstaticintcase1(){intx;try{x=1;returnx;}finally{x=3;}}}#Output The output of the above code can simply conclude: return is executed before finally. Let's take a look at what happens at the bytecode level. The following intercepts part of the bytecode of the case1 method, and compares the source code to annotate the meaning of each instruction in

Use Java's String.replace() function to replace characters (strings) in a string Use Java's String.replace() function to replace characters (strings) in a string Jul 25, 2023 pm 05:16 PM

Replace characters (strings) in a string using Java's String.replace() function In Java, strings are immutable objects, which means that once a string object is created, its value cannot be modified. However, you may encounter situations where you need to replace certain characters or strings in a string. At this time, we can use the replace() method in Java's String class to implement string replacement. The replace() method of String class has two types:

2w words detailed explanation String, yyds 2w words detailed explanation String, yyds Aug 24, 2023 pm 03:56 PM

Hello everyone, today I will share with you the basic knowledge of Java: String. Needless to say the importance of the String class, it can be said to be the most used class in our back-end development, so it is necessary to talk about it.

See all articles