Hướng dẫn dùng convertcase net trong PHP

[PHP 4 >= 4.3.0, PHP 5, PHP 7, PHP 8]

mb_convert_casePerform case folding on a string

Description

mb_convert_case[string $string, int $mode, ?string $encoding = null]: string

Parameters

string

The string being converted.

mode

The mode of the conversion. It can be one of MB_CASE_UPPER, MB_CASE_LOWER, MB_CASE_TITLE, MB_CASE_FOLD, MB_CASE_UPPER_SIMPLE, MB_CASE_LOWER_SIMPLE, MB_CASE_TITLE_SIMPLE, MB_CASE_FOLD_SIMPLE.

encoding

The encoding parameter is the character encoding. If it is omitted or null, the internal character encoding value will be used.

Return Values

A case folded version of string converted in the way specified by mode.

Changelog

VersionDescription
7.3.0 Added support for MB_CASE_FOLD, MB_CASE_UPPER_SIMPLE, MB_CASE_LOWER_SIMPLE, MB_CASE_TITLE_SIMPLE, and MB_CASE_FOLD_SIMPLE as mode.

Examples

Example #1 mb_convert_case[] example

Example #2 mb_convert_case[] example with non-Latin UTF-8 text

Notes

By contrast to the standard case folding functions such as strtolower[] and strtoupper[], case folding is performed on the basis of 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 a-umlaut [ä].

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

See Also

  • mb_strtolower[] - Make a string lowercase
  • mb_strtoupper[] - Make a string uppercase
  • strtolower[] - Make a string lowercase
  • strtoupper[] - Make a string uppercase
  • ucfirst[] - Make a string's first character uppercase
  • ucwords[] - Uppercase the first character of each word in a string

alNzy

2 years ago

You can use this function to fix problems related to Turkish "ı", "I", "i", "İ" characters. This function also replaces the weird "i̇" character with regular "i" character ["i̇ => i"].

function mb_convert_case_tr[$str, $type, $encoding = "UTF-8"]
{

  switch [$type] {
    case "u":
    case "upper":
    case MB_CASE_UPPER:
      $type = MB_CASE_UPPER;
      break;
    case "l":
    case "lower":
    case MB_CASE_LOWER:
      $type = MB_CASE_LOWER;
      break;
    case "t":
    case "title":
    case MB_CASE_TITLE:
      $type = MB_CASE_TITLE;
      break;
  }

  $str = str_replace["i", "İ", $str];
  $str = str_replace["I", "ı", $str];

  $str = mb_convert_case[$str, $type, $encoding];
  $str = str_replace["i̇", "i", $str];

  return $str;
}

agash at freemail dot hu

13 years ago

as the previouly posted version of this function doesn't handle UTF-8 characters, I simply tried to replace ucfirst to mb_convert_case, but then any previous case foldings were lost while looping through delimiters.
So I decided to do an mb_convert_case on the input string [it also deals with words is uppercase wich may also be problematic when doing case-sensitive search], and do the rest of checking after that.

As with mb_convert_case, words are capitalized, I also added lowercase convertion for the exceptions, but, for the above mentioned reason, I left ucfirst unchanged.

Now it works fine for utf-8 strings as well, except for string delimiters followed by an UTF-8 character ["Mcádám" is unchanged, while "mcdunno's" is converted to "McDunno's" and "ökrös-TÓTH éDUa" in also put in the correct form]

I use it for checking user input on names and addresses, so exceptions list contains some hungarian words too.

Chủ Đề