PHP – 使用 mb_check_encoding() 檢查字串是否對指定的編碼有效


在 PHP 中,**mb_check_encoding()** 函式用於檢查給定的字串是否對指定的編碼有效。此函式檢查指定的位元組流對於指定的編碼是否有效。

語法

bool mb_check_encoding(str $value=null, str $encoding=null)

**注意:**該函式將檢查所述位元組流對於所述編碼是否有效。如果給定的值為陣列型別,則所有鍵和值都將遞迴驗證。它避免了無效編碼攻擊。

引數

**mb_check_encoding()** 接受兩個引數:**$value** 和 **$encoding**。

  • **$value−** 用於檢查位元組流或陣列,如果省略,則檢查從請求開始的所有輸入。

  • **$encoding−** 用於預期編碼。

返回值

**mb_get_encoding()** 成功時返回 True,失敗時返回 False。

示例

 即時演示

<?php
   // Using mb_check_encoding function to check
   //if the strings are valid
   $bool = mb_check_encoding ("Hello world", "ASCII");

   // returns true
   var_dump($bool);
?>

輸出

bool(true)

**注意:**在以前的版本中,**mb_check_encoding()** 函式不允許使用可空值,但從 PHP 8.0 開始,我們可以使用可空值和編碼。從 PHP 7.2 開始,**mb_check_encoding** 函式接受陣列作為值,之前它僅支援字串。

更新於: 2021年8月23日

807 次檢視

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.