php字符串處理之全角半角轉(zhuǎn)換,php字符串全角半角
Jun 13, 2016 am 08:45 AMphp字符串處理之全角半角轉(zhuǎn)換,php字符串全角半角
半角全角的處理是字符串處理的常見問題,本文嘗試為大家提供一個思路。
一、概念
全角字符unicode編碼從65281~65374 (十六進(jìn)制 0xFF01 ~ 0xFF5E)
半角字符unicode編碼從33~126 (十六進(jìn)制 0x21~ 0x7E)
空格比較特殊,全角為 12288(0x3000),半角為 32 (0x20)
而且除空格外,全角/半角按unicode編碼排序在順序上是對應(yīng)的
所以可以直接通過用+-法來處理非空格數(shù)據(jù),對空格單獨(dú)處理
二、實(shí)現(xiàn)思路
1. 找到目標(biāo)unicode的字符,可以使用正則表達(dá)式解決
2. 修改unicode編碼
三、實(shí)現(xiàn)
1. 首先是兩個unicode與字符的轉(zhuǎn)換函數(shù):
<span> 1</span> <span>/*</span><span>* </span><span> 2</span> <span> * 將unicode轉(zhuǎn)換成字符 </span><span> 3</span> <span> * @param int $unicode </span><span> 4</span> <span> * @return string UTF-8字符 </span><span> 5</span> <span> *</span><span>*/</span> <span> 6</span> <span>function</span> unicode2Char(<span>$unicode</span><span>){ </span><span> 7</span> <span>if</span>(<span>$unicode</span> < 128) <span>return</span> <span>chr</span>(<span>$unicode</span><span>); </span><span> 8</span> <span>if</span>(<span>$unicode</span> < 2048) <span>return</span> <span>chr</span>((<span>$unicode</span> >> 6) + 192) . <span> 9</span> <span>chr</span>((<span>$unicode</span> & 63) + 128<span>); </span><span>10</span> <span>if</span>(<span>$unicode</span> < 65536) <span>return</span> <span>chr</span>((<span>$unicode</span> >> 12) + 224) . <span>11</span> <span>chr</span>(((<span>$unicode</span> >> 6) & 63) + 128) . <span>12</span> <span>chr</span>((<span>$unicode</span> & 63) + 128<span>); </span><span>13</span> <span>if</span>(<span>$unicode</span> < 2097152) <span>return</span> <span>chr</span>((<span>$unicode</span> >> 18) + 240) . <span>14</span> <span>chr</span>(((<span>$unicode</span> >> 12) & 63) + 128) . <span>15</span> <span>chr</span>(((<span>$unicode</span> >> 6) & 63) + 128) . <span>16</span> <span>chr</span>((<span>$unicode</span> & 63) + 128<span>); </span><span>17</span> <span>return</span> <span>false</span><span>; </span><span>18</span> <span> } </span><span>19</span> <span>20</span> <span>/*</span><span>* </span><span>21</span> <span> * 將字符轉(zhuǎn)換成unicode </span><span>22</span> <span> * @param string $char 必須是UTF-8字符 </span><span>23</span> <span> * @return int </span><span>24</span> <span> *</span><span>*/</span> <span>25</span> <span>function</span> char2Unicode(<span>$char</span><span>){ </span><span>26</span> <span>switch</span> (<span>strlen</span>(<span>$char</span><span>)){ </span><span>27</span> <span>case</span> 1 : <span>return</span> <span>ord</span>(<span>$char</span><span>); </span><span>28</span> <span>case</span> 2 : <span>return</span> (<span>ord</span>(<span>$char</span>{1}) & 63) | <span>29</span> ((<span>ord</span>(<span>$char</span>{0}) & 31) << 6<span>); </span><span>30</span> <span>case</span> 3 : <span>return</span> (<span>ord</span>(<span>$char</span>{2}) & 63) | <span>31</span> ((<span>ord</span>(<span>$char</span>{1}) & 63) << 6) | <span>32</span> ((<span>ord</span>(<span>$char</span>{0}) & 15) << 12<span>); </span><span>33</span> <span>case</span> 4 : <span>return</span> (<span>ord</span>(<span>$char</span>{3}) & 63) | <span>34</span> ((<span>ord</span>(<span>$char</span>{2}) & 63) << 6) | <span>35</span> ((<span>ord</span>(<span>$char</span>{1}) & 63) << 12) | <span>36</span> ((<span>ord</span>(<span>$char</span>{0}) & 7) << 18<span>); </span><span>37</span> <span>default</span> : <span>38</span> <span>trigger_error</span>('Character is not UTF-8!', <span>E_USER_WARNING</span><span>); </span><span>39</span> <span>return</span> <span>false</span><span>; </span><span>40</span> <span> } </span><span>41</span> }
2. 全角轉(zhuǎn)半角
<span> 1</span> <span>/*</span><span>* </span><span> 2</span> <span> * 全角轉(zhuǎn)半角 </span><span> 3</span> <span> * @param string $str </span><span> 4</span> <span> * @return string </span><span> 5</span> <span> *</span><span>*/</span> <span> 6</span> <span>function</span> sbc2Dbc(<span>$str</span><span>){ </span><span> 7</span> <span>return</span> <span>preg_replace</span><span>( </span><span> 8</span> <span>//</span><span> 全角字符 </span> <span> 9</span> '/[\x{3000}\x{ff01}-\x{ff5f}]/ue', <span>10</span> <span>//</span><span> 編碼轉(zhuǎn)換 </span><span>11</span> <span> // 0x3000是空格,特殊處理,其他全角字符編碼-0xfee0即可以轉(zhuǎn)為半角</span> <span>12</span> '($unicode=char2Unicode(\'\0\')) == 0x3000 ? " " : (($code=$unicode-0xfee0) > 256 ? unicode2Char($code) : chr($code))', <span>13</span> <span>$str</span> <span>14</span> <span> ); </span><span>15</span> }
3. 半角轉(zhuǎn)全角
<span> 1</span> <span>/*</span><span>* </span><span> 2</span> <span> * 半角轉(zhuǎn)全角 </span><span> 3</span> <span> * @param string $str </span><span> 4</span> <span> * @return string </span><span> 5</span> <span> *</span><span>*/</span> <span> 6</span> <span>function</span> dbc2Sbc(<span>$str</span><span>){</span> <span> 7</span> <span>return</span> <span>preg_replace</span><span>( </span><span> 8</span> <span>//</span><span> 半角字符 </span> <span> 9</span> '/[\x{0020}\x{0020}-\x{7e}]/ue', <span>10</span> <span>//</span><span> 編碼轉(zhuǎn)換 </span><span>11</span> <span> // 0x0020是空格,特殊處理,其他半角字符編碼+0xfee0即可以轉(zhuǎn)為全角</span> <span>12</span> '($unicode=char2Unicode(\'\0\')) == 0x0020 ? unicode2Char(0x3000) : (($code=$unicode+0xfee0) > 256 ? unicode2Char($code) : chr($code))', <span>13</span> <span>$str</span> <span>14</span> <span> ); </span><span>15</span> }
四、測試
示例代碼:
<span>1</span> <span>$a</span> = 'abc12 345'<span>; </span><span>2</span> <span>$sbc</span> = dbc2Sbc(<span>$a</span><span>); </span><span>3</span> <span>$dbc</span> = sbc2Dbc(<span>$sbc</span><span>); </span><span>4</span> <span>5</span> <span>var_dump</span>(<span>$a</span>, <span>$sbc</span>, <span>$dbc</span>);
結(jié)果:
<span>1</span> <span>string</span>(9) "abc12 345" <span>2</span> <span>string</span>(27) "abc12?。常矗?quot; <span>3</span> <span>string</span>(9) "abc12 345"
?

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

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

The difference between full-width and half-width: 1. The space occupied by characters; 2. The representation of characters; 3. The status of the input method; 4. Essential differences; 5. The use of system commands. Detailed introduction: 1. Character occupation space, full-width characters occupy two standard character positions, while half-width characters occupy one standard character position; 2. Character representation, full-width refers to one character occupying two standard character positions, whether it is a Chinese character Or other types of characters, and half-width means that a character occupies a standard character position, usually used for English letters, numbers, symbols, etc.

In daily life, we often encounter the problem of full-width and half-width, but few people may have a deep understanding of their meaning and difference. Full-width and half-width are actually concepts of character encoding methods, and they have special applications in computer input, editing, typesetting, etc. This article will delve into the differences between full-width and half-width, switching techniques, and real-life applications. First of all, the definitions of full-width and half-width in the field of Chinese characters are: a full-width character occupies one character position, and a half-width character occupies half a character position. In a computer, pass

When we use the Chinese input method to type, there are two ways: full-width and half-width. Among them, there are many friends who don’t know the difference between full-width and half-width. Let’s take a look at the full-width and half-width input methods. Make the difference. Any punctuation mark can be used in the half-width state, and there are no special restrictions on spaces. In the full-width state, the function of the space bar changes. After switching to the full-width state and pressing the space bar, we will find that the distance between characters becomes large, and this gap is more obvious than in the half-width state. In addition, the display of English letters is also different in the full-width state. In the half-width state, the typed English letters are of normal size. However, once we switch to full-width mode, we will find that the typed English letters become bold and enlarged. in the whole

Full-width and half-width refer to two different input states in the Chinese input method. Full-width means that each character occupies a full-width character position, while half-width means that each character occupies a half-width character position. When using a computer to input Chinese, sometimes you need to switch between full-width and half-width to adapt to different input scenarios. Next, we will introduce several commonly used full-width and half-width switching methods, so that everyone can easily learn how to switch between full-width and half-width in Chinese input. Method 1: Use shortcut keys to switch between full-width and half-width. In most Chinese input method software, you can use

In the modern Internet era, we often enter and edit text on computers. In this process, we sometimes encounter the problem of switching between full-width and half-width. Full-width and half-width refer to the size of the position occupied by characters. Full-width characters occupy one character width, while half-width characters occupy half a character width. The correct use of full-width and half-width is very important to ensure the formatting and uniformity of text. Therefore, it is necessary to master the switching skills of full-width and half-width. First, let us understand the concepts of full-width and half-width. Full-width characters generally refer to

When users usually use input methods to insert text, they may notice differences between letters or symbols. This is mainly due to the different switching between half-width and full-width characters. Here, we will introduce you in detail how to quickly realize the conversion between full-width and half-width. Shortcut key solution for switching between full-width and half-width in windows: Alt+Shift This is the most commonly used key to switch between full-width and half-width input modes. In the Chinese input environment, pressing this key will switch to English mode, otherwise it will switch to Chinese mode. If there are multiple input methods installed on your computer, you can also use this key to easily switch between input methods. Solution 2: Ctrl+Shift is also among the shortcut keys for switching between full-width and half-width. This key combination is pressed in English input mode

Full-width spaces and half-width spaces are two common space characters in Chinese and Japanese. They have different functional characteristics in typesetting, text editing, and input methods. This article will explain in detail the characteristics and application scenarios of full-width spaces and half-width spaces, helping readers better understand and use these two space characters. First, let’s understand the characteristics of full-width spaces. The encoding of full-width spaces in Unicode is U+3000. The width is the same as that of ordinary Chinese characters. It is usually used for blank spaces in Chinese typesetting. Usage scenarios for full-width spaces include but

Title: How to easily switch between full-width and half-width modes in input methods When using computers daily, we often need to switch between full-width and half-width to adapt to different input scenarios and needs. Full-width mode is generally used to input Chinese characters, English characters, punctuation marks, numbers, etc., while half-width mode is more suitable for inputting English and numbers. However, many people may be confused when using input methods and don't know how to easily switch between full-width and half-width. This article will introduce some simple methods to help you easily switch between full-width and half-width input methods.
