PHP 5.4.31 Released

mb_strtolower

(PHP 4 >= 4.3.0, PHP 5)

mb_strtolowerMet tous les caractères en minuscules

Description

string mb_strtolower ( string $str [, string $encoding = mb_internal_encoding() ] )

Retourne la chaîne str après avoir converti tous les caractères alphabétiques en minuscules.

Liste de paramètres

str

La chaîne à mettre en minuscule.

encoding

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

Valeurs de retour

Retourne la chaîne str dont tous les caractères alphabétiques ont été mis en minuscule.

Unicode

Pour plus d'informations sur les propriétés de l'Unicode, voyez » http://www.unicode.org/unicode/reports/tr21/.

Contrairement à strtolower(), le concept de caractère 'alphabétique' est déterminé par les propriétés Unicode. De ce fait, le comportement de cette fonction n'est pas modifié par les configurations locales, et elle peut convertir tout les caractères qui sont considérés comme alphabétiques comme le c cédille (ç).

Exemples

Exemple #1 Exemple avec mb_strtolower()

<?php
$str 
"Marie A Un Petit Agneau Et Elle L'Aime BEAUCOUP.";
$str mb_strtolower($str);
echo 
$str// marie a un petit agneau et elle l'aime beaucoup
?>

Exemple #2 Exemple avec mb_strtolower() avec du texte UTF-8 non latin

<?php
$str 
"Τάχιστη αλώπηξ βαφής ψημένη γη, δρασκελίζει υπέρ νωθρού κυνός";
$str mb_strtolower($str'UTF-8');
echo 
$str// Affiche τάχιστη αλώπηξ βαφής ψημένη γη, δρασκελίζει υπέρ νωθρού κυνός
?>

Voir aussi

add a note add a note

User Contributed Notes 5 notes

up
4
Philipp H
6 years ago
Note that mb_strtolower() is very SLOW, if you have a database connection, you may want to use it to convert your strings to lower case. Even latin1/9 (iso-8859-1/15) and other encodings are possible.

Have a look at my simple benchmark:

<?php

$text
= "Lörem ipßüm dölör ßit ämet, cönßectetüer ädipißcing elit. Sed ligülä. Präeßent jüßtö tellüß, grävidä eü, tempüß ä, mättiß nön, örci. Näm qüiß lörem. Näm äliqüet elit ßed elit. Phäßellüß venenätiß jüßtö eget enim. Dönec nißl. Pröin mättiß venenätiß jüßtö. Sed äliqüäm pörtä örci. Cräß elit nißl, cönvälliß qüiß, tincidünt ät, vehicülä äccümßän, ödiö. Sed möleßtie. Etiäm mölliß feügiät elit. Veßtibülüm änte ipßüm primiß in fäücibüß örci lüctüß et ültriceß pößüere cübiliä Cüräe; Mäecenäß nön nüllä.";

// mb_strtolower()
$timeMB = microtime(true);    
             
    for(
$i=0;$i<30000;$i++)
       
$lower = mb_strtolower("$text/no-cache-$i");

$timeMB = microtime(true) - $timeMB;

// MySQL lower()
$timeSQL = microtime(true);   

   
mysql_query("set names latin1");              
    for(
$i=0;$i<30000;$i++) {
       
$r = mysql_fetch_row(mysql_query("select lower('$text/no-cache-$i')"));
       
$lower = $r[0];
    }

$timeSQL = microtime(true) - $timeSQL;

echo
"mb: ".sprintf("%.5f",$timeMB)." sek.<br />";
echo
"sql: ".sprintf("%.5f",$timeSQL)." sek.<br />";

// Result on my notebook:
// mb: 11.50642 sek.
// sql: 5.44143 sek.

?>
up
1
akniep at linklift dot net
2 years ago
Please, note that when using with UTF-8 mb_strtolower will only convert upper case characters to lower case which are marked with the Unicode property "Upper case letter" ("Lu"). However, there are also letters such as "Letter numbers" (Unicode property "Nl") that also have lower case and upper case variants. These characters will not be converted be mb_strtolower!

Example:
The Roman letters Ⅰ, Ⅱ, Ⅲ, ..., Ⅿ (UTF-8 code points 8544 through 8559) also exist in their respective lower case variants ⅰ, ⅱ, ⅲ, ..., ⅿ (UTF-8 code points 8560 through 8575) and should, in my opinion, also be converted by mb_strtolower, but they are not!

Big internet-companies (like Google) do match both variants as semantically equal (since the representations only differ in case).

Since I was not finding any proper solution in the internet on how to map all UTF8-strings to their lowercase counterpart in PHP, I offer the following hard-coded extended mb_strtolower function for UTF-8 strings:

The function wraps the existing function mb_strtolower() and additionally replaces uppercase UTF8-characters for which there is a lowercase representation. Since there is no proper Unicode uppercase and lowercase character-table in the internet that I was able to find, I checked the first million UTF8-characters against the Google-search and -KeywordTool and identified the following 78 characters as uppercase-characters, not being replaced by mb_strtolower, but having a UTF8 lowercase counterpart.

<?php

//the numbers in the in-line-comments display the characters' Unicode code-points (CP).
function strtolower_utf8_extended( $utf8_string )
{
   
$additional_replacements    = array
        (
"Dž"    => "dž"        //   453 ->   454
       
, "Lj"    => "lj"        //   456 ->   457
       
, "Nj"    => "nj"        //   459 ->   460
       
, "Dz"    => "dz"        //   498 ->   499
       
, "Ϸ"    => "ϸ"        //  1015 ->  1016
       
, "Ϲ"    => "ϲ"        //  1017 ->  1010
       
, "Ϻ"    => "ϻ"        //  1018 ->  1019
       
, "ᾈ"    => "ᾀ"        //  8072 ->  8064
       
, "ᾉ"    => "ᾁ"        //  8073 ->  8065
       
, "ᾊ"    => "ᾂ"        //  8074 ->  8066
       
, "ᾋ"    => "ᾃ"        //  8075 ->  8067
       
, "ᾌ"    => "ᾄ"        //  8076 ->  8068
       
, "ᾍ"    => "ᾅ"        //  8077 ->  8069
       
, "ᾎ"    => "ᾆ"        //  8078 ->  8070
       
, "ᾏ"    => "ᾇ"        //  8079 ->  8071
       
, "ᾘ"    => "ᾐ"        //  8088 ->  8080
       
, "ᾙ"    => "ᾑ"        //  8089 ->  8081
       
, "ᾚ"    => "ᾒ"        //  8090 ->  8082
       
, "ᾛ"    => "ᾓ"        //  8091 ->  8083
       
, "ᾜ"    => "ᾔ"        //  8092 ->  8084
       
, "ᾝ"    => "ᾕ"        //  8093 ->  8085
       
, "ᾞ"    => "ᾖ"        //  8094 ->  8086
       
, "ᾟ"    => "ᾗ"        //  8095 ->  8087
       
, "ᾨ"    => "ᾠ"        //  8104 ->  8096
       
, "ᾩ"    => "ᾡ"        //  8105 ->  8097
       
, "ᾪ"    => "ᾢ"        //  8106 ->  8098
       
, "ᾫ"    => "ᾣ"        //  8107 ->  8099
       
, "ᾬ"    => "ᾤ"        //  8108 ->  8100
       
, "ᾭ"    => "ᾥ"        //  8109 ->  8101
       
, "ᾮ"    => "ᾦ"        //  8110 ->  8102
       
, "ᾯ"    => "ᾧ"        //  8111 ->  8103
       
, "ᾼ"    => "ᾳ"        //  8124 ->  8115
       
, "ῌ"    => "ῃ"        //  8140 ->  8131
       
, "ῼ"    => "ῳ"        //  8188 ->  8179
       
, "Ⅰ"    => "ⅰ"        //  8544 ->  8560
       
, "Ⅱ"    => "ⅱ"        //  8545 ->  8561
       
, "Ⅲ"    => "ⅲ"        //  8546 ->  8562
       
, "Ⅳ"    => "ⅳ"        //  8547 ->  8563
       
, "Ⅴ"    => "ⅴ"        //  8548 ->  8564
       
, "Ⅵ"    => "ⅵ"        //  8549 ->  8565
       
, "Ⅶ"    => "ⅶ"        //  8550 ->  8566
       
, "Ⅷ"    => "ⅷ"        //  8551 ->  8567
       
, "Ⅸ"    => "ⅸ"        //  8552 ->  8568
       
, "Ⅹ"    => "ⅹ"        //  8553 ->  8569
       
, "Ⅺ"    => "ⅺ"        //  8554 ->  8570
       
, "Ⅻ"    => "ⅻ"        //  8555 ->  8571
       
, "Ⅼ"    => "ⅼ"        //  8556 ->  8572
       
, "Ⅽ"    => "ⅽ"        //  8557 ->  8573
       
, "Ⅾ"    => "ⅾ"        //  8558 ->  8574
       
, "Ⅿ"    => "ⅿ"        //  8559 ->  8575
       
, "Ⓐ"    => "ⓐ"        //  9398 ->  9424
       
, "Ⓑ"    => "ⓑ"        //  9399 ->  9425
       
, "Ⓒ"    => "ⓒ"        //  9400 ->  9426
       
, "Ⓓ"    => "ⓓ"        //  9401 ->  9427
       
, "Ⓔ"    => "ⓔ"        //  9402 ->  9428
       
, "Ⓕ"    => "ⓕ"        //  9403 ->  9429
       
, "Ⓖ"    => "ⓖ"        //  9404 ->  9430
       
, "Ⓗ"    => "ⓗ"        //  9405 ->  9431
       
, "Ⓘ"    => "ⓘ"        //  9406 ->  9432
       
, "Ⓙ"    => "ⓙ"        //  9407 ->  9433
       
, "Ⓚ"    => "ⓚ"        //  9408 ->  9434
       
, "Ⓛ"    => "ⓛ"        //  9409 ->  9435
       
, "Ⓜ"    => "ⓜ"        //  9410 ->  9436
       
, "Ⓝ"    => "ⓝ"        //  9411 ->  9437
       
, "Ⓞ"    => "ⓞ"        //  9412 ->  9438
       
, "Ⓟ"    => "ⓟ"        //  9413 ->  9439
       
, "Ⓠ"    => "ⓠ"        //  9414 ->  9440
       
, "Ⓡ"    => "ⓡ"        //  9415 ->  9441
       
, "Ⓢ"    => "ⓢ"        //  9416 ->  9442
       
, "Ⓣ"    => "ⓣ"        //  9417 ->  9443
       
, "Ⓤ"    => "ⓤ"        //  9418 ->  9444
       
, "Ⓥ"    => "ⓥ"        //  9419 ->  9445
       
, "Ⓦ"    => "ⓦ"        //  9420 ->  9446
       
, "Ⓧ"    => "ⓧ"        //  9421 ->  9447
       
, "Ⓨ"    => "ⓨ"        //  9422 ->  9448
       
, "Ⓩ"    => "ⓩ"        //  9423 ->  9449
       
, "𐐦"    => "𐑎"        // 66598 -> 66638
       
, "𐐧"    => "𐑏"        // 66599 -> 66639
       
);
   
   
$utf8_string    = mb_strtolower( $utf8_string, "UTF-8");
   
   
$utf8_string    = strtr( $utf8_string, $additional_replacements );
   
    return
$utf8_string;
}
//strtolower_utf8_extended()

?>
up
1
fisharebest at gmail dot com
2 years ago
There is not a one-to-one correspondence between upper and lower case letters.

Turkish is a good example of this.  In Turkish, the letter I/i has a dotted-upper-case form (İ) and a dotless-lower-case form (ı).

https://en.wikipedia.org/wiki/Dotted_and_dotless_I

This means that you cannot correctly convert between upper-case and lower-case without also knowing the locale of the data.

Since the function does not let you specify a locale, you should only use this function for text written in languages that follow the same orthography as English.

Although it does handle some digraphs, such as the Dutch ij (ij), it does not handle others, such as the Polish dz (ʣ).
up
1
Ken Shiro
3 years ago
[If you get this error:]
Fatal error: Call to undefined function: mb_strtolower() in ????.php on line ??

The PHP mbstring extension, which is required to handle international character sets, is not available on your server. Check your PHP configuration and make sure that PHP has been compiled with --enable-mbstring.

It's also apply to
Call to undefined function mb_eregi() / mb_strtolower()
up
1
btherl at yahoo dot com dot au
8 years ago
If you use this function on a unicode string without telling PHP that it is unicode, then you will corrupt your string.  In particular, the uppercase 'A' with tilde, common in 2-byte UTF-8 characters, is converted to lowercase 'a' with tilde.

This can be handled correctly by:
$str = mb_strtolower($str, mb_detect_encoding($str));

Or if you know your data is UTF-8, just use the string "UTF-8" as the second argument.

You should check also that mb_detect_encoding() is checking the encodings you want it to check, and is detecting the correct encodings.
To Top