CakeFest 2024: The Official CakePHP Conference

mb_convert_kana

(PHP 4 >= 4.0.6, PHP 5, PHP 7, PHP 8)

mb_convert_kanaカナを("全角かな"、"半角かな"等に)変換する

説明

mb_convert_kana(string $string, string $mode = "KV", ?string $encoding = null): string

文字列 string に関して「半角」-「全角」変換を行います。 この関数は、日本語のみで使用可能です。

パラメータ

string

変換される文字列。

mode

変換オプション。

以下のオプションを組み合わせて指定します。

使用可能な変換オプション
オプション 意味
r 「全角」英字を「半角」に変換します。
R 「半角」英字を「全角」に変換します。
n 「全角」数字を「半角」に変換します。
N 「半角」数字を「全角」に変換します。
a 「全角」英数字を「半角」に変換します。
A 「半角」英数字を「全角」に変換します ("a", "A" オプションに含まれる文字は、U+0022, U+0027, U+005C, U+007Eを除く U+0021 - U+007E の範囲です)。
s 「全角」スペースを「半角」に変換します(U+3000 -> U+0020)。
S 「半角」スペースを「全角」に変換します(U+0020 -> U+3000)。
k 「全角カタカナ」を「半角カタカナ」に変換します。
K 「半角カタカナ」を「全角カタカナ」に変換します。
h 「全角ひらがな」を「半角カタカナ」に変換します。
H 「半角カタカナ」を「全角ひらがな」に変換します。
c 「全角カタカナ」を「全角ひらがな」に変換します。
C 「全角ひらがな」を「全角カタカナ」に変換します。
V 濁点付きの文字を一文字に変換します。"K", "H" と共に使用します。

encoding

encoding パラメータには文字エンコーディングを指定します。省略した場合、もしくは null の場合は、 内部文字エンコーディングを使用します。

戻り値

変換後の文字列を返します。

エラー / 例外

異なる mode の組み合わせが無効な場合 (例: "sS") は、 ValueError がスローされます。

変更履歴

バージョン 説明
8.2.0 異なる mode の組み合わせが無効な場合、 ValueError がスローされるようになりました。
8.0.0 encoding は、nullable になりました。

例1 mb_convert_kana() の例

<?php
/* 「仮名」を全て「全角カタカナ」に変換します */
$str = mb_convert_kana($str, "KVC");

/* 「半角カタカナ」を「全角カタカナ」に変換し、「全角」英数字を「半角」
に変換します。 */
$str = mb_convert_kana($str, "KVa");
?>

add a note

User Contributed Notes 4 notes

up
2
dn at littlealf dot net
20 years ago
It seems that mb_convert_kana() doesn't convert symbols such as ' (single quoatation). It affected my program when I insert data into database.

So, I've found 2 ways to solve this.
1) Use Javascript to convert those non-supported symbols before sending query string to your php page.
2) Use php function, str_replace $str, to replace those non-supported symbols. For example, $str = str_replace("'", "?", $str);
where the first single quot is half-width(han kaku), and the second one is full-width(zen kaku).
up
0
jdavide222 at hotmail dot com
11 years ago
to make this function work you have to add following two lines above this function.

mb_language("Ja");
mb_internal_encoding("utf-8");

mb_convert_kana($_POST['something_value'], "rna");
up
0
d at safetypub dot org
18 years ago
I didn't find Japanese sorting function.
(mb_sort_kana or something)
Only SJIS encoding treats hankaku kata kana,
I can't post here copy & paste version script.
I'm sorry...
You will replace comments with appropriate japanese string,
and write exception handlings between lines as you like.

<?php
mb_internal_encoding
("SJIS");

$moji = /*"aiueo...wawon -?.," <- serialized hankaku kata kana 50 on hyou goes here*/;
$moji .= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxwz0123456789";

for(
$i = 0; isset($moji[$i]); $i++){
$pittan[$moji[$i]] = $i;
}

function
cmp($a, $b)
{
if (
$a == $b) {
return
0;
}
return
iter(mb_convert_kana($a, "askh") ,mb_convert_kana($b, "askh"), 0);
}

function
iter($a, $b, $i){
global
$pittan;
if(!isset(
$a[$i]) || !isset($b[$i])){
return (isset(
$b[$i]))? -1 : 1;
}
if (
$pittan[$a[$i]] == $pittan[$b[$i]]){
return
iter($a, $b, ++$i);
}
return ((
$pittan[$a[$i]]) < ($pittan[$b[$i]]))? -1 : 1;
}

echo
"<pre>";
$arr = array(/*some japanese array here*/);
usort($arr, "cmp");
var_dump($arr);
?>
up
-43
sohel62 at yahoo dot com
16 years ago
I did that manually when I was not getting expected result using mb_convert_kana().
<?php
/*
* @author Taslim Mazumder Sohel
* @deprecated 1.0 - 2008/02/19
*
*/
function han_kaku_to_jen_kaku($str){
$replace_of = array('ヴ','ガ','ギ','グ',
'ゲ','ゴ','ザ','ジ',
'ズ','ゼ','ゾ','ダ',
'ヂ','ヅ','デ','ド',
'バ','ビ','ブ','ベ',
'ボ','パ','ピ','プ','ペ','ポ');
$replace_by = array('ヴ','ガ','ギ','グ',
'ゲ','ゴ','ザ','ジ',
'ズ','ゼ','ゾ','ダ',
'ヂ','ヅ','デ','ド',
'バ','ビ','ブ','ベ',
'ボ','パ','ピ','プ','ペ','ポ');
$_result = str_replace($replace_of, $replace_by, $str);

$replace_of = array('ア','イ','ウ','エ','オ',
'カ','キ','ク','ケ','コ',
'サ','シ','ス','セ','ソ',
'タ','チ','ツ','テ','ト',
'ナ','ニ','ヌ','ネ','ノ',
'ハ','ヒ','フ','ヘ','ホ',
'マ','ミ','ム','メ','モ',
'ヤ','ユ','ヨ','ラ','リ',
'ル','レ','ロ','ワ','ヲ',
'ン','ァ','ィ','ゥ','ェ',
'ォ','ヵ','ヶ','ャ','ュ',
'ョ','ッ','、','。','ー',
'「','」','゙','゚');
$replace_by = array('ア','イ','ウ','エ','オ',
'カ','キ','ク','ケ','コ',
'サ','シ','ス','セ','ソ',
'タ','チ','ツ','テ','ト',
'ナ','ニ','ヌ','ネ','ノ',
'ハ','ヒ','フ','ヘ','ホ',
'マ','ミ','ム','メ','モ',
'ヤ','ユ','ヨ','ラ','リ',
'ル','レ','ロ','ワ','ヲ',
'ン','ァ','ィ','ゥ','ェ',
'ォ','ヶ','ヶ','ャ','ュ',
'ョ','ッ','、','。','ー',
'「','」','”','');
$_result = str_replace($replace_of, $replace_by, $_result);
return
$_result;
}
?>
To Top