In PHP multi-byte string processing function, mb_convert_kana is a very practical function, mainly used for various conversions of Japanese characters, such as the conversion of full and half-width, the conversion of Kana's Hiragana business cardikatana, etc. Its usage form is roughly as follows:
string mb_convert_kana(string $string, string $mode = "KV", ?string $encoding = null)
Where, the $mode parameter is a combination of multiple flag letters used to specify the conversion method. People often wonder if the order of multiple flags in $mode is different, will the conversion result be different? This article will analyze this issue in detail based on specific cases.
Let’s briefly list a few commonly used logo letters and meanings:
K : Convert full-width Katakana to half-width Katakana
V : Restore the voiced and semi-voiced symbols in half-width Katakana to the complete voiced Pseudonym
H : Convert full-width English numbers into half-width English numbers
A : Convert full-width alphanumeric and symbols into half-width
R : Convert half-width English numbers to full-width English numbers
s : Convert full-width space to half-width space
These flags can be used in combination, for example, "KV" means that K operation is performed first and then V operation.
When dealing with the $mode flags , the order is actually very important. Because each flag represents an independent conversion step, the conversion is performed sequentially. If you do one conversion first and then another, the result is often different from the other in the reverse order.
For example:
$input = "カタカナ"; // Full-width katakana
// order1:FirstKbackV
$output1 = mb_convert_kana($input, "KV");
// order2:FirstVbackK
$output2 = mb_convert_kana($input, "VK");
echo "orderKVresult: $output1\n";
echo "orderVKresult: $output2\n";
The order of "KV" is to convert the full-width Katakana to half-width Katakana (K), and then convert the voiced symbols in half-width Katakana to full-width Katakana (V), which is usually the correct and commonly used order.
"VK" first tries to restore the voiced symbol in half-width Katakana (V), but since the character is still full-width Katakana at this time, the V conversion will not work. Then the K conversion will convert the full-width Katakana into half-width, and the final result is different from "KV".
Assume that the input string contains half-width Katakana with voiced notation:
$input = "??"; // Half-width Katakana“ガ”Split into Katakana+Voice mark
// 按order使用不同模式
$output1 = mb_convert_kana($input, "KV"); // FirstKbackV
$output2 = mb_convert_kana($input, "VK"); // FirstVbackK
echo "enter: $input\n";
echo "KV模式转换back: $output1\n";
echo "VK模式转换back: $output2\n";
Using "KV" , K will first turn the full corner into half of the corner (here is already half corner, K basically has no effect), and then V will restore the half corner Katakana plus the voiced symbol to the complete kana "Ge".
Using "VK" , V first tries to restore the voiced symbol (successfully), and then K performs the conversion (here the full-width will be turned into half a corner, but at this time it is a full-width pseudonym, converting it into half a corner), and the final result of the two will be different.
The order of flags in the $mode parameter of mb_convert_kana directly affects the successive steps of the conversion process.
Different orders will cause different changes in character states, which will affect the final output result.
Only by correctly understanding the meaning and conversion order of each flag can we ensure that the conversion effect is in line with expectations.
Common combinations such as "KV" , which first converts the full-width Katakana into half-width, and then restores the voiced symbols, which is a logical and common order.
For more details, please refer to the mb_convert_kana function document in the official PHP manual.