Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : Multibyte String Functions : mb_strtolower

mb_strtolower

Make a string lowercase (PHP 4 >= 4.3.0, PHP 5)
string mb_strtolower ( string str [, string encoding] )

Example 1406. mb_strtolower() example

<?php
$str
= "Mary Had A Little Lamb and She LOVED It So";
$str = mb_strtolower($str);
echo
$str; // Prints mary had a little lamb and she loved it so
?>

Code Examples / Notes » mb_strtolower

philipp h

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.
?>


btherl

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.


Change Language


Follow Navioo On Twitter
mb_check_encoding
mb_convert_case
mb_convert_encoding
mb_convert_kana
mb_convert_variables
mb_decode_mimeheader
mb_decode_numericentity
mb_detect_encoding
mb_detect_order
mb_encode_mimeheader
mb_encode_numericentity
mb_ereg_match
mb_ereg_replace
mb_ereg_search_getpos
mb_ereg_search_getregs
mb_ereg_search_init
mb_ereg_search_pos
mb_ereg_search_regs
mb_ereg_search_setpos
mb_ereg_search
mb_ereg
mb_eregi_replace
mb_eregi
mb_get_info
mb_http_input
mb_http_output
mb_internal_encoding
mb_language
mb_output_handler
mb_parse_str
mb_preferred_mime_name
mb_regex_encoding
mb_regex_set_options
mb_send_mail
mb_split
mb_strcut
mb_strimwidth
mb_stripos
mb_stristr
mb_strlen
mb_strpos
mb_strrchr
mb_strrichr
mb_strripos
mb_strrpos
mb_strstr
mb_strtolower
mb_strtoupper
mb_strwidth
mb_substitute_character
mb_substr_count
mb_substr
eXTReMe Tracker