PHP Doku:: Make a string lowercase - function.mb-strtolower.html

Verlauf / Chronik / History: (14) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzUnterstützung menschlicher Sprache und ZeichenkodierungMultibyte StringMultibyte String Funktionenmb_strtolower

Ein Service von Reinhard Neidl - Webprogrammierung.

Multibyte String Funktionen

<<mb_strstr

mb_strtoupper>>

mb_strtolower

(PHP 4 >= 4.3.0, PHP 5)

mb_strtolowerMake a string lowercase

Beschreibung

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

Returns str with all alphabetic characters converted to lowercase.

Parameter-Liste

str

The string being lowercased.

encoding

Der encoding Parameter legt das Zeichenencoding fest. Wird er nicht übergeben so wird das interne Zeichenencoding genutzt.

Rückgabewerte

str with all alphabetic characters converted to lowercase.

Unicode

For more information about the Unicode properties, please see » http://www.unicode.org/unicode/reports/tr21/.

By contrast to strtolower(), 'alphabetic' is determined by the Unicode character properties. Thus the behaviour of this function is not affected by locale settings and it can convert any characters that have 'alphabetic' property, such as A-umlaut (Ä).

Beispiele

Beispiel #1 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
?>

Beispiel #2 mb_strtolower() example with non-Latin UTF-8 text

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

Siehe auch


4 BenutzerBeiträge:
- Beiträge aktualisieren...
Ken Shiro
31.07.2010 11:07
[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()
pozmu at the same as user name dot net
21.01.2008 4:55
btherl at yahoo dot com dot au - it also corrupt other characters, like Polish national characters (which can be found also in iso-8859-2 encoding).

So if your locale != charset used in php always specify charset as second argument:

<? echo mb_strtolower("WSTĘP","UTF-8"); ?>
Philipp H
1.11.2007 19:11
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 at yahoo dot com dot au
16.11.2005 6:12
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.



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)