PHP 8.3.4 Released!

IntlChar::tolower

(PHP 7, PHP 8)

IntlChar::tolowerUnicode 文字を小文字にする

説明

public static IntlChar::tolower(int|string $codepoint): int|string|null

指定した文字を、同等の小文字にマップします。 同等の小文字が存在しない場合、指定された文字そのものを返します。

パラメータ

codepoint

コードポイントを表す int 型の値 (例: U+2603 SNOWMAN を表す 0x2603)、あるいは UTF-8 文字列としてエンコードされた文字 (例: "\u{2603}")。

戻り値

存在する場合に、コードポイントの Simple_Lowercase_Mapping の値を返します。 存在しない場合は、コードポイントそのものを返します。 失敗した場合は、null を返します。

戻り値の型は int になります。ただし、コードポイントを UTF-8 文字列で渡した場合は別で、その場合の返り値の型は文字列になります。失敗した場合は、null を返します。

例1 さまざまなコードポイントの例

<?php
var_dump
(IntlChar::tolower("A"));
var_dump(IntlChar::tolower("a"));
var_dump(IntlChar::tolower("Φ"));
var_dump(IntlChar::tolower("φ"));
var_dump(IntlChar::tolower("1"));
var_dump(IntlChar::tolower(ord("A")));
var_dump(IntlChar::tolower(ord("a")));
?>

上の例の出力は以下となります。

string(1) "a"
string(1) "a"
string(2) "φ"
string(2) "φ"
string(1) "1"
int(97)
int(97)

参考

add a note

User Contributed Notes 1 note

up
0
Patanjali
3 years ago
The other function I wrote to replace mb_strtolower may not work properly, as it erroneously equated graphemes with codepoints.

tolower, like many IntlChar methods, works specifically on codepoints, so requires a codepoint iterator to isolate each.

Also, because in tolower, if there is no lowercase version of the codepoint, the supplied one is returned, so there is no need to specially test for alphabetic codepoints before conversion.

<?php
function u_tolower($text=''){
// if blank, return blank (don't waste CPU cycles)
if($text==''){return'';}

// create the codepoint break iterator to identify the start of each codepoint
$iterator=IntlBreakIterator::createCodePointInstance();

// load the text
$iterator->setText($text);

// using a parts iterator to extract each codepoint itself, convert and append it to the new string
$newtext='';
foreach(
$iterator->getPartsIterator() as $codepoint){$newtext.=IntlChar::tolower($codepoint);}

// return converted text
return $newtext;
}
?>
To Top