CakeFest 2024: The Official CakePHP Conference

mb_convert_kana

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

mb_convert_kanaConvertit un "kana" en un autre ("zen-kaku", "han-kaku" et plus)

Description

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

Effectue une conversion "han-kaku" - "zen-kaku" sur la chaîne string. Cette fonction est uniquement utile pour les japonais.

Liste de paramètres

string

La chaîne à convertir.

mode

L'option de conversion.

Spécifiez les conversions en combinant les valeurs suivantes.

Options de conversions disponibles
Option Signification
r Convertit l'alphabet "zen-kaku" en "han-kaku"
R Convertit l'alphabet "han-kaku" en "zen-kaku"
n Convertit les nombres "zen-kaku" en "han-kaku"
N Convertit les nombres "han-kaku" en "zen-kaku"
a Convertit les nombres et alphabet "zen-kaku" en "han-kaku"
A Convertit les nombres et alphabet "zen-kaku" en "han-kaku". (Les caractères inclus dans les options "a", "A" sont U+0021 - U+007E en excluant U+0022, U+0027, U+005C, U+007E)
s Convertit "zen-kaku" en "han-kaku" (U+3000 -> U+0020)
S Convertit "han-kaku" en "zen-kaku" (U+0020 -> U+3000)
k Convertit "zen-kaku kata-kana" en "han-kaku kata-kana"
K Convertit "han-kaku kata-kana" en "zen-kaku kata-kana"
h Convertit "zen-kaku hira-gana" en "han-kaku kata-kana"
H Convertit "han-kaku kata-kana" en "zen-kaku hira-gana"
c Convertit "zen-kaku kata-kana" en "zen-kaku hira-gana"
C Convertit "zen-kaku hira-gana" en "zen-kaku kata-kana"
V Supprime les notations vocales, et les convertit en caractères. À utiliser avec "K","H"

encoding

Le paramètre encoding est l'encodage des caractères. S'il est omis ou null, l'encodage de caractères interne sera utilisé.

Valeurs de retour

La chaîne convertie.

Erreurs / Exceptions

Lance une ValueError si la combinaison de différents mode n'est pas valide. Par exemple "sS".

Historique

Version Description
8.2.0 Une ValueError est désormais lancée si la combinaison de différents modes n'est pas valide.
8.0.0 encoding est désormais nullable.

Exemples

Exemple #1 Exemple avec mb_convert_kana()

<?php
/* Convertit tous les "kana" en "zen-kaku" "kata-kana" */
$str = mb_convert_kana($str, "KVC");

/* Convertit "han-kaku" "kata-kana" en "zen-kaku" "kata-kana"
et "zen-kaku" alphanumérique en "han-kaku" */
$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