How do i convert all letters to lowercase in php?

(PHP 4, PHP 5, PHP 7, PHP 8)

strtolowerMake a string lowercase

Description

strtolower(string $string): string

Note that 'alphabetic' is determined by the current locale. This means that e.g. in the default "C" locale, characters such as umlaut-A (Ä) will not be converted.

Parameters

string

The input string.

Return Values

Returns the lowercased string.

Examples

Example #1 strtolower() example

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

Notes

Note: This function is binary-safe.

See Also

  • strtoupper() - Make a string uppercase
  • ucfirst() - Make a string's first character uppercase
  • ucwords() - Uppercase the first character of each word in a string
  • mb_strtolower() - Make a string lowercase

marcin at maydesign dot pl

12 years ago

strtolower(); doesn't work for polish chars

<?php strtolower("mĄkA"); ?>
will return: mĄka;

the best solution - use mb_strtolower()

<?php mb_strtolower("mĄkA",'UTF-8'); ?>
will return: mąka

coder at bulgaria dot bg

13 years ago

for cyrillic and UTF 8 use  mb_convert_case

exampel

<?php
$string
= "Австралия";
$string = mb_convert_case($string, MB_CASE_LOWER, "UTF-8");
echo
$string; //output is: австралия
?>

dbers26 at gmail dot com

13 years ago

the function  arraytolower will create duplicate entries since keys are case sensitive. 

<?php
$array
= array('test1' => 'asgAFasDAAd', 'TEST2' => 'ASddhshsDGb', 'TeSt3 '=> 'asdasda@asdadadASDASDgh'); $array = arraytolower($array);
?>
/*
Array
(
    [test1] => asgafasdaad
    [TEST2] => ASddhshsDGb
    [TeSt3] => asdasda@asdadadASDASDgh
    [test2] => asddhshsdgb
    [test3] => asdasda@asdadadasdasdgh
)
*/

I prefer this method

<?php
 
function arraytolower($array, $include_leys=false) {

        if(

$include_leys) {
      foreach(
$array as $key => $value) {
        if(
is_array($value))
         
$array2[strtolower($key)] = arraytolower($value, $include_leys);
        else
         
$array2[strtolower($key)] = strtolower($value);
      }
     
$array = $array2;
    }
    else {
      foreach(
$array as $key => $value) {
        if(
is_array($value))
         
$array[$key] = arraytolower($value, $include_leys);
        else
         
$array[$key] = strtolower($value);  
      }
    }

        return

$array;
  }
?>

which when used like this

<?php
$array
= $array = array('test1' => 'asgAFasDAAd', 'TEST2' => 'ASddhshsDGb', 'TeSt3 '=> 'asdasda@asdadadASDASDgh'); $array1 = arraytolower($array);
$array2 = arraytolower($array,true); print_r($array1);
print_r($array2);
?>

will give output of

Array
(
    [test1] => asgafasdaad
    [TEST2] => asddhshsdgb
    [TeSt3] => asdasda@asdadadasdasdgh
)
Array
(
    [test1] => asgafasdaad
    [test2] => asddhshsdgb
    [test3] => asdasda@asdadadasdasdgh
)

rodrigoATsistemasparainternetDOTcomDOTbr

14 years ago

<?php
function fullLower($str){
  
// convert to entities
  
$subject = htmlentities($str,ENT_QUOTES);
  
$pattern = '/&([a-z])(uml|acute|circ';
  
$pattern.= '|tilde|ring|elig|grave|slash|horn|cedil|th);/e';
  
$replace = "'&'.strtolower('\\1').'\\2'.';'";
  
$result = preg_replace($pattern, $replace, $subject);
  
// convert from entities back to characters
  
$htmltable = get_html_translation_table(HTML_ENTITIES);
   foreach(
$htmltable as $key => $value) {
     
$result = ereg_replace(addslashes($value),$key,$result);
   }
   return(
strtolower($result));
}

echo

fullLower("Ã É Ò Õ ÚÙÛ");//results ã é ò õ úùû
//adapted from fullUpper on strtoupper manual
?>

patricia at steuerungb dot de

14 years ago

When you're not sure, how the current locale is set, you might find the following function useful. It's strtolower for utf8-formatted text:

<?php
function strtolower_utf8($inputString) {
   
$outputString    = utf8_decode($inputString);
   
$outputString    = strtolower($outputString);
   
$outputString    = utf8_encode($outputString);
    return
$outputString;
}
?>

It's not suitable for every occasion, but it surely gets in handy. I use it for lowering German 'Umlauts' like ä and ö.

rok dot kralj at gmail dot com

15 years ago

Slovenian characters

<?php
   
function strtolower_slovenian($string)
    {
       
$low=array("Č" => "č", "Ž" => "ž", "Š" => "š");
        return
strtolower(strtr($string,$low));
    }
?>

bkimble at ebaseweb dot com

19 years ago

Heres a small function I wrote to stop people from submitting data that is ALL IN CAPS SO THEY CAN GET MORE ATTENTION THAT THE REST OF THE USER SUBMITTED DATA on my website :) If you can make it better, by all means do so. This function splits up words delimited by a space, and makes only the first letter of each word capitalized. You can easily modify it so it's only the very first word of the string. I've also added some exceptions so you don't make things like roman numerals look like "Iii" or "Xcmii" or something.

function RemoveShouting($string)
{
$lower_exceptions = array(
        "to" => "1", "a" => "1", "the" => "1", "of" => "1"
);

$higher_exceptions = array( 
        "I" => "1", "II" => "1", "III" => "1", "IV" => "1", 
        "V" => "1", "VI" => "1", "VII" => "1", "VIII" => "1",
        "XI" => "1", "X" => "1"
);

$words = split(" ", $string);
$newwords = array();
foreach ($words as $word)
{
  if (!$higher_exceptions[$word]) $word = strtolower($word);
  if (!$lower_exceptions[$word]) $word[0] = strtoupper($word[0]);
  array_push($newwords, $word);
}
return join(" ", $newwords);
}

BK

marco at recchiuti dot it

15 years ago

Maybe it is not so elegant, but it Works.
It's just a fast Idea and it is what I need.
Any hacks for other characters (link !, ? etc etc) should help.

function RemoveShouting($string)
{
     $frase = "";   
     $astri = explode(".", $string);
     foreach ($astri as $elem)
    $frase .= " ".ucfirst(trim(strtolower($elem))).". ";

           return trim($frase);
}

Cheers!
M

Patrick

16 years ago

If you're considering using the below unhtmlentities function from phpContrib, I would suggest this one as an alternative:

<?php
function unhtmlentities($string)
{
  
// replace numeric entities
  
$string = preg_replace('~&#x([0-9a-f]+);~ei', 'chr(hexdec("\\1"))', $string);
  
$string = preg_replace('~&#([0-9]+);~e', 'chr(\\1)', $string);
  
// replace literal entities
  
$trans_tbl = get_html_translation_table(HTML_ENTITIES);
  
$trans_tbl = array_flip($trans_tbl);
   return
strtr($string, $trans_tbl);
}
?>

That was copied exactly from the html_entity_decode manual page.  It'll handle numeric entities correctly, the below function won't.

mhuggins57 at yahoo dot com

17 years ago

There's a ucfirst "function" to make the first character uppercase, but there's no "lcfirst" function to make the first character lowercase.  Here's my own code to accomplish this.

<?
function lcfirst($str) {
    return strtolower(substr($str, 0, 1)) . substr($str, 1);
}
?>

I found this particularly useful for generating XML nodes with the Reflection class.

RCube

11 years ago

To convert an entire array to lower, I prefer this method;

<?php
function arraytolower(array $array, $round = 0){
  return
unserialize(strtolower(serialize($array)));
}
?>

3 lines of code seem a lot less overhead than 10-40.
If there's any intrinsic problem with this method, please post it.

kmcdermott at perimeterinstitute dot ca

17 years ago

To do case insensitive comparisons in a database, strtolower() can be a quick and dirty solution:

$Sql = "SELECT * FROM tablename WHERE LOWER(column_name) = '".strtolower($my_var)."'";

leha_grobov

13 years ago

the strtolower version to support most amount of languages including russian, french and so on:

<?php
function strtolower_utf8($string){
 
$convert_to = array(
   
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u",
   
"v", "w", "x", "y", "z", "à", "á", "â", "ã", "ä", "å", "æ", "ç", "è", "é", "ê", "ë", "ì", "í", "î", "ï",
   
"ð", "ñ", "ò", "ó", "ô", "õ", "ö", "ø", "ù", "ú", "û", "ü", "ý", "а", "б", "в", "г", "д", "е", "ё", "ж",
   
"з", "и", "й", "к", "л", "м", "н", "о", "п", "р", "с", "т", "у", "ф", "х", "ц", "ч", "ш", "щ", "ъ", "ы",
   
"ь", "э", "ю", "я"
 
);
 
$convert_from = array(
   
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U",
   
"V", "W", "X", "Y", "Z", "À", "Á", "Â", "Ã", "Ä", "Å", "Æ", "Ç", "È", "É", "Ê", "Ë", "Ì", "Í", "Î", "Ï",
   
"Ð", "Ñ", "Ò", "Ó", "Ô", "Õ", "Ö", "Ø", "Ù", "Ú", "Û", "Ü", "Ý", "А", "Б", "В", "Г", "Д", "Е", "Ё", "Ж",
   
"З", "И", "Й", "К", "Л", "М", "Н", "О", "П", "Р", "С", "Т", "У", "Ф", "Х", "Ц", "Ч", "Ш", "Щ", "Ъ", "Ъ",
   
"Ь", "Э", "Ю", "Я"
 
);

  return

str_replace($convert_from, $convert_to, $string);
}
?>

spiceee ddotty spiceee com

16 years ago

If you ever need to strtolower a string with href tags on it and doesn't want to mess with the characters inside a tag, this is for you.

<?phpfunction loweroutsidetags ($str) {
   
$chars = preg_split ("//", $str);
   
$tolower = true;
   
$str = '';

    foreach (

$chars as $k) {
        if (
$k == '<') { $tolower = false; }
        if (
$tolower) { $k = strtolower ($k); }
       
$str .= $k;   
        if (
$k == '>') { $tolower = true; }
    }
    return
$str;
}
?>

this:
echo loweroutsidetags('aALalala <a href="?q=CASEsENSITIVINESSinURLSareSTUPID">')

will give:
aalalala <a href="?q=CASEsENSITIVINESSinURLSareSTUPID">

Daniel Klein

6 years ago

When the locale is set to "utf8" or "C" I can't get strtolower() to convert accented characters encoded as UTF-8, so here is my workaround.

<?php
function tolower($string) {
  if (
$old_locale = setlocale(LC_CTYPE, 0)) {
   
// 'fr_FR.ISO-8859-1' for *nix, 'French_France.1252' fallback for Windows
   
if ($newlocale = setlocale(LC_CTYPE, 'fr_FR.ISO-8859-1', 'French_France.1252')) {
     
preg_match('/\.(?<encoding>.+)$/', $newlocale, $match);
     
$string = utf8_encode(strtolower(utf8_decode($string)));
    }
   
setlocale(LC_CTYPE, $old_locale);
  }
  return
$string;
}

print(

tolower('DÉJÀ-VU'));  // prints déjà-vu
?>

Change the language-country codes if you want some language other than French.

How do I convert a string to all lowercase?

The toLowerCase method converts a string to lowercase letters. The toLowerCase() method doesn't take in any parameters. Strings in JavaScript are immutable. The toLowerCase() method converts the string specified into a new one that consists of only lowercase letters and returns that value.

How do you capitalize all letters in PHP?

The strtoupper() function converts a string to uppercase. Note: This function is binary-safe. Related functions: strtolower() - converts a string to lowercase.

How do you lowercase in MySQL?

MySQL LOWER() Function The LOWER() function converts a string to lower-case. Note: The LCASE() function is equal to the LOWER() function.

How do I check if a letter is uppercase in PHP?

The ctype_upper() function in PHP used to check each and every character of a given string is in uppercase or not. If the string in upper case then it returns TRUE otherwise returns False.