Caps to small in 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() , то что символ является буквой определяется на основании свойств символа Юникода. Таким образом на поведение функции не влияют региональные настройки системы, а также функция может преобразовывать символы, имеющие буквенные особенности, такие как а-умляут (ä).

За дополнительной информацией о свойствах Юникода обращайтесь в » 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.

Читайте также:  Работа по ssh python

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

Читайте также:  Jquery tooltips with html

$utf8_string = mb_strtolower ( $utf8_string , «UTF-8» );

$utf8_string = strtr ( $utf8_string , $additional_replacements );

return $utf8_string ;
> //strtolower_utf8_extended()

Источник

How to Convert All Caps to Sentence Case in PHP: Complete Guide with Examples

Learn how to convert a string from all caps to sentence case in PHP using various built-in functions. This article covers best practices and helpful tips for string manipulation in PHP.

  • Using strtolower() and ucwords() functions for case conversion
  • Using mb_convert_case() function for case conversion with UTF-8 encoding
  • Php case change (upper to lowercase
  • Other useful string manipulation functions in PHP
  • Best practices for string manipulation in PHP
  • Additional resources for string manipulation in PHP
  • Other simple code examples for converting all caps to sentence case in PHP
  • Conclusion
  • How can I use case sentence in PHP?
  • How to convert capital letters to small letters in PHP?
  • How to use Ucfirst in PHP?
  • How to make string capital in PHP?

As a web developer, you will often encounter the need to manipulate strings in PHP, including changing their case. One common task is converting a string from all caps to sentence case. In this article, we’ll discuss how to convert all caps to sentence case in PHP using various functions. We’ll also cover important points to consider when working with string manipulation in php and helpful tips for best practices.

Using strtolower() and ucwords() functions for case conversion

The strtolower() function converts a string to all lowercase, while ucwords() converts the first character of each word in the string to uppercase. To convert a string from all caps to sentence case, we can first use strtolower() function, and then use ucwords() function. Here’s an example code:

$string = "HELLO WORLD"; $string = strtolower($string); $string = ucwords($string); echo $string; // Output: Hello World 

This method works for ASCII characters only. If you have special characters in the string, this method may not work as expected. In that case, you can use the mb_convert_case() function.

Using mb_convert_case() function for case conversion with UTF-8 encoding

The mb_convert_case() function is used to handle special characters in UTF-8 encoding. This function can be used to convert a string from all caps to sentence case. Here’s an example code:

$string = "ΓΕΙΑ ΣΟΥ ΚΟΣΜΕ"; $string = mb_convert_case($string, MB_CASE_TITLE, "UTF-8"); echo $string; // Output: Γεια Σου Κοσμε 

In this example, we pass the string, the case mode MB_CASE_TITLE , and the encoding UTF-8 as parameters to the mb_convert_case() function. This method works for both ASCII and non-ASCII characters.

Php case change (upper to lowercase

Now in this «php tutorial for beginners in English» video tutorial [php case change (upper to Duration: 12:59

Other useful string manipulation functions in PHP

PHP provides various built-in functions for string manipulation. Here are some other useful functions that you can use for case conversion:

  • ucfirst() — converts the first character of a string to uppercase
  • lcfirst() — converts the first character of a string to lowercase
  • strtoupper() — converts a string to all caps
  • mb_strtoupper() — converts a string to all caps with UTF-8 encoding
  • mb_strtolower() — converts a string to all lowercase with UTF-8 encoding
$string = "hello world"; $string = ucfirst($string); echo $string; // Output: Hello world$string = "HELLO WORLD"; $string = strtolower($string); echo $string; // Output: hello world$string = "ΓΕΙΑ ΣΟΥ ΚΟΣΜΕ"; $string = mb_strtolower($string, "UTF-8"); echo $string; // Output: γεια σου κοσμε 

Best practices for string manipulation in PHP

When working with string manipulation in PHP, it is important to consider best practices and potential issues. Here are some tips for best practices:

  • Sanitize user input before using string manipulation functions to prevent security vulnerabilities. You can use the filter_var() function to sanitize user input.
  • Use a consistent naming convention , such as camel case or snake case, to improve code readability.
  • Consider the impact of case sensitivity when working with databases or APIs that may have different naming conventions.
$string = $_POST['input']; $string = filter_var($string, FILTER_SANITIZE_STRING); 

Additional resources for string manipulation in PHP

PHP provides various built-in functions for string manipulation, and there are also additional resources that you can use to improve your string manipulation skills . Here are some resources:

  • PHP 8 introduces new string functions, such as str_contains() and str_starts_with() . You can check the official PHP documentation website for more details.
  • PHP frameworks, such as Laravel and Symfony, provide additional helper functions for string manipulation.
$text = "This is a sample text."; if (str_contains($text, "sample"))

Other simple code examples for converting all caps to sentence case in PHP

In Php case in point, php change sting to caps code sample

$lowercase = "this is lower case"; $uppercase = strtoupper($lowercase);echo $uppercase;

In Php , for instance, php uppercase with accent code sample

Читайте также:  Php fpm address already in use

In Php as proof, convert all text in php to uppercase code sample

Conclusion

In this article, we discussed how to convert all caps to sentence case in PHP using various functions. We also covered important points to consider when working with string manipulation in PHP and helpful tips for best practices. By using these functions and following best practices, you can manipulate strings in PHP more efficiently and effectively.

Frequently Asked Questions — FAQs

PHP is a server-side scripting language specifically designed for web development. It is popular due to its ease of use, flexibility, and wide range of built-in functions for various tasks, including string manipulation.

What are the different functions used for case conversion in PHP?

PHP provides several built-in functions for case conversion, including strtolower(), strtoupper(), ucwords(), ucfirst(), mb_strtolower(), mb_strtoupper(), and mb_convert_case().

Can case conversion functions be used with special characters in PHP?

Yes, the mb_convert_case() function is specifically designed to handle special characters in UTF-8 encoding.

What is the importance of sanitizing user input before using string manipulation functions in PHP?

Sanitizing user input is important to prevent security vulnerabilities, such as SQL injection attacks.

How can consistent naming conventions improve code readability in PHP?

Consistent naming conventions, such as camel case or snake case, make it easier to understand and maintain code, especially in larger projects with multiple developers.

Are there any additional resources available for string manipulation in PHP?

Yes, the official PHP documentation website provides a cheatsheet for PHP string functions, and PHP frameworks like Laravel and Symfony provide additional helper functions for string manipulation.

Источник

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