Сделать все буквы маленькими php

mb_strtolower

Возвращает строку string , буквенные символы в которой приведены к нижнему регистру.

Список параметров

Параметр encoding представляет собой символьную кодировку. Если он опущен или равен null , вместо него будет использовано значение внутренней кодировки.

Возвращаемые значения

str , буквы в которой приведены к нижнему регистру.

Примеры

Пример #1 Пример использования mb_strtolower()

$str = «У Мэри Был Маленький Ягнёнок и Она Его Очень ЛЮБИЛА» ;
$str = mb_strtolower ( $str );
echo $str ; // Выведет у мэри был маленький ягнёнок и она его очень любила
?>

Пример #2 Пример использования mb_strtolower() с нелатинскими буквами

$str = «Τάχιστη αλώπηξ βαφής ψημένη γη, δρασκελίζει υπέρ νωθρού κυνός» ;
$str = mb_strtolower ( $str , ‘UTF-8’ );
echo $str ; // Выведет τάχιστη αλώπηξ βαφής ψημένη γη, δρασκελίζει υπέρ νωθρού κυνός
?>

Примечания

В отличие от strtolower() , то что символ является буквой определяется на основании свойств символа Юникода. Таким образом на поведение функции не влияют региональные настройки системы, а также функция может преобразовывать символы, имеющие буквенные особенности, такие как а-умляут (ä).

Читайте также:  Python list sort by function

За дополнительной информацией о свойствах Юникода обращайтесь в » http://www.unicode.org/reports/tr21/.

Смотрите также

  • mb_strtoupper() — Приведение строки к верхнему регистру
  • mb_convert_case() — Производит смену регистра символов в строке
  • strtolower() — Преобразует строку в нижний регистр

User Contributed Notes 6 notes

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.

//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()

Источник

Замена регистра в строках PHP

Список PHP-функций для изменения регистра символов в строках и примеры их использования.

Проверка, является ли буква прописной или строчной

Функция ctype_upper($string) – определяет, являются ли все буквы в строке в верхнем регистре.

$str = 'Ы'; if (ctype_upper($str)) < echo 'Заглавная'; >else

Вариант для кириллицы в кодировке UTF-8:

$str = 'Ы'; if (mb_strtolower($str) !== $str) < echo 'Заглавная'; >else < echo 'строчная'; >// Выведется «Заглавная»

Пример определения регистра для первой буквы в строке:

$text = 'Привет мир!'; $chr = mb_substr($text, 0, 1); if (mb_strtolower($chr) !== $chr) < echo 'Заглавная'; >else < echo 'строчная'; >// Выведется «Заглавная»

Первая заглавная буква

ucfirst($string) — преобразует первый символ строки в верхний регистр.

$text = 'привет Мир!'; echo ucfirst($text); 

Для UTF-8:

if(!function_exists('mb_ucfirst')) < function mb_ucfirst($str) < $fc = mb_strtoupper(mb_substr($str, 0, 1)); return $fc . mb_substr($str, 1); >> $text = 'привет Мир!'; echo mb_ucfirst($text); // Привет Мир!

Первая строчная

ucfirst($string) — преобразует первый символ строки в верхний регистр.

$text = 'Привет Мир!'; echo lcfirst($text); 

Для UTF-8:

if(!function_exists('mb_lcfirst')) < function mb_lcfirst($str) < $fc = mb_strtolower(mb_substr($str, 0, 1)); return $fc . mb_substr($str, 1); >> $text = 'Привет Мир!'; echo mb_lcfirst($text); // привет Мир!

Все заглавные буквы

Для UTF-8:

$text = 'привет Мир!'; echo mb_strtoupper($text); // ПРИВЕТ МИР!

Все строчные буквы

$text = 'Привет Мир!'; echo strtolower($text);

Для UTF-8:

$text = 'Привет Мир!'; echo mb_strtolower($text); // привет мир!

Заглавная буква в каждом слове

$text = 'привет мир!'; echo ucwords($text);

Для UTF-8:

if(!function_exists('mb_ucwords')) < function mb_ucwords($str) < $str = mb_convert_case($str, MB_CASE_TITLE, "UTF-8"); return ($str); >> $text = 'привет мир!'; echo mb_ucwords($text); // Привет Мир!

Инверсия регистра

function mb_flip_case($string) < $characters = preg_split('/(?$char) < if (mb_strtolower($char, "UTF-8") != $char) < $char = mb_strtolower($char, 'UTF-8'); >else < $char = mb_strtoupper($char, 'UTF-8'); >$characters[$key] = $char; > return implode('', $characters); > $text = 'Привет Мир!'; echo mb_flip_case($text); // пРИВЕТ мИР!

Комментарии 1

function invertCase($text)
$string = »;
/*
Решение №1
$mb_strlen = mb_strlen($text);
$i = $mb_strlen;
while ($i > 0) $i—;
$char = mb_substr($text, $i, 1);
$char = (mb_strtolower($char) === $char) ? mb_strtoupper($char) : mb_strtolower($char);
$string = $char.$string;
>
*/
// Решение №2
$arr = mb_str_split($text, 1);
foreach ($arr as $char) $char = (mb_strtolower($char) === $char) ? mb_strtoupper($char) : mb_strtolower($char);
$string .= $char;
>
return $string;
>

Авторизуйтесь, чтобы добавить комментарий.

Источник

mb_strtolower

Возвращает строку str , буквенные символы в которой приведены к нижнему регистру.

Список параметров

Параметр encoding представляет собой символьную кодировку. Если он опущен, вместо него будет использовано значение внутренней кодировки.

Возвращаемые значения

str , буквы в которой приведены к нижнему регистру.

Юникод

За дополнительной информацией о свойствах Юникода обращайтесь в » http://www.unicode.org/unicode/reports/tr21/.

В отличие от strtolower() , то что символ является буквой определяется на основании свойств символа Юникода. Таким образом на поведение функции не влияют региональные настройки системы, а также функция может преобразовывать символы, имеющие буквенные особенности, такие как А-умляут (Ä).

Примеры

Пример #1 Пример использования mb_strtolower()

$str = «У Мэри Был Маленький Ягненок и Она Его Очень ЛЮБИЛА» ;
$str = mb_strtolower ( $str );
echo $str ; // Выведет у мэри был маленький ягненок и она его очень любила
?>

Пример #2 Пример использования mb_strtolower() с нелатинскими буквами

$str = «Τάχιστη αλώπηξ βαφής ψημένη γη, δρασκελίζει υπέρ νωθρού κυνός» ;
$str = mb_strtolower ( $str , ‘UTF-8’ );
echo $str ; // Выведет τάχιστη αλώπηξ βαφής ψημένη γη, δρασκελίζει υπέρ νωθρού κυνός
?>

Смотрите также

  • mb_strtoupper() — Приведение строки к верхнему регистру
  • mb_convert_case() — Производит смену регистра символов в строке
  • strtolower() — Преобразует строку в нижний регистр

Источник

How to change case in PHP strings to upper, lower, sentence, etc

As a developer, you work with strings of different nature in day-to-day projects. Some may be a word long, a phrase, a sentence, or even comprising thousands of words.

It is easy to work on and change the case on shorter strings manually, eg. you can edit the string and make a sentence begin with an uppercase letter, to change an all uppercase text to a sentence case, etc.

In the case of long strings of texts, this will not be an easy task to accomplish and may take too long. The good news is that most programming languages have a way to easily convert and manipulate the case in a string.

In this article, you will learn how to convert strings to different cases using PHP language.

Converting a string to lowercase in PHP

To change all the alphabetical characters in a string to lowercase, we use the PHP strtolower() function.

Converting a string to uppercase in PHP

To change all the alphabetical characters in a string to uppercase, we use the PHP strtoupper() function.

Converting the first character in a string to uppercase in PHP

To change the first alphabetical characters of a string to uppercase, we use ucfirst() function.

Converting the first character of each word to uppercase in PHP

To change the first character of every word in a string in PHP, we use ucwords() function.

Converting the string to sentence case in PHP

All sentences start with an uppercase(capital) letter and end with either a full stop (.), a question mark (?), or an exclamation mark (!).

If our string comprises only one sentence, then converting it to a sentence case is very simple as we only use the ucfirst() function to convert its first character to uppercase.

Which programming language do you love most?

However, that will not be the case with a string comprising of multiple sentences.

— We split the string into an array of sentences using the preg_split() function;

— We loop through all the sentences in a for each loop;

— Convert the string characters in each sentence to lower case using the strtolower() function;

— Convert the first character of every sentence to uppercase using the ucfirst() function;

— Concatenate all our sentences to form back the original string, now with a sentence case.

Hello world! I am a programmer. I like programming in php.

From the above example, you can skip the conversion to lower case if you just want all the sentences to start with an uppercase without affecting the cases of other words/characters.

Conclusion

In this article, you have learned how to convert PHP strings to upper case, lower case, the first character of every word to upper case, the first character of the string to upper case, and also how to convert a combination of multiple sentences into sentence case, where every first character of every sentence starts with an upper case.

It is my hope that the article was simple enough to follow along and easy to understand and implement.

To get notified when we add more incredible content to our site, feel free to subscribe to our free email newsletter.

Источник

Оцените статью