PHP – 透過 mb_convert_case() 實現字串摺疊大小寫


mb_convert_case() 是 PHP 中的一個內建函式,用於對給定的字串執行摺疊大小寫操作。

語法

string mb_convert_case(str $string, int $mode, str $encoding)

引數

mb_convert_case() 接受三個引數:$string、$mode  $encoding,以便對字串執行摺疊大小寫操作。

  • $string− 此引數用於返回要轉換的字串。

  • $mode: mode 引數用於轉換模式。它可用於多位元組字串轉換為 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: 此引數是字元編碼。如果省略或為 null,則將使用內部字元編碼值

返回值

mb_convert_case() 用於返回轉換的字串模式。

注意: 從 PHP 7.3.0 起,一些多位元組函式被新增為模式,例如 MB_CASE_FOLD、MB_CASE_UPPER_SIMPLE、MB_CASE_LOWER_SIMPLE、MB_CASE_TITLE_SIMPLE 和 MB_CASE_FOLD_SIMPLE。

示例 1

 即時演示

<?php
   $string = "Hello World!, Welcome to the online Tutorial";

   // convert above string in upper case
   $string = mb_convert_case($string, MB_CASE_UPPER, "UTF-8");
   echo $string;


   // It will convert given string in lower case
   $string = mb_convert_case($string, MB_CASE_LOWER, "UTF-8");
   echo $string;
?>

輸出

HELLO WORLD!, WELCOME TO THE ONLINE TUTORIALhello world!, welcome to the online tutorial

示例 2

 即時演示

<?php
   $string = "Hello World!, Welcome to the online Tutorial";

   // MB_CASE_TITLE is used
   $string = mb_convert_case($string, MB_CASE_TITLE, "UTF-8");
   echo $string;

   // MB_CASE_UPPER_SIMPLE convert string in upper case
   $string = mb_convert_case($string, MB_CASE_UPPER_SIMPLE, "UTF-8");
   echo $string;
?>

輸出

Hello World!, Welcome To The Online TutorialHELLO WORLD!, WELCOME TO THE ONLINE TUTORIAL

更新於:2021-08-23

382 次瀏覽

職業生涯快速起步

完成課程獲得認證

入門
廣告
© . All rights reserved.