成人无码视频,亚洲精品久久久久av无码,午夜精品久久久久久毛片,亚洲 中文字幕 日韩 无码

資訊專欄INFORMATION COLUMN

PHP之mb_check_encoding使用

tracymac7 / 2481人閱讀

摘要:檢查字符串在指定的編碼里是否有效檢查指定的字節(jié)流在指定的編碼里是否有效。它能有效避免所謂的無效編碼攻擊。要檢查的字節(jié)流。成功時返回,或者在失敗時返回。輸出空輸出編碼的字符串設置文件編碼為博客園和。

mb_check_encoding

(PHP 4 >= 4.4.3, PHP 5 >= 5.1.3, PHP 7)

mb_check_encoding — Check if the string is valid for the specified encoding

mb_check_encoding — 檢查字符串在指定的編碼里是否有效

Description
bool mb_check_encoding ([ string $var = NULL [, string $encoding = mb_internal_encoding() ]] )
// Checks if the specified byte stream is valid for the specified encoding. 
// It is useful to prevent so-called "Invalid Encoding Attack".

// 檢查指定的字節(jié)流在指定的編碼里是否有效。它能有效避免所謂的“無效編碼攻擊(Invalid Encoding Attack)”。
Parameters var

The byte stream to check. If it is omitted, this function checks all the input from the beginning of the request.

要檢查的字節(jié)流。如果省略了這個參數,此函數會檢查所有來自最初請求所有的輸入。

encoding

The expected encoding.

期望的編碼。

Return Values

Returns TRUE on success or FALSE on failure.

成功時返回 TRUE, 或者在失敗時返回 FALSE。

Examples
 設置文件編碼為gbk*/
$str = "博客園和github。";
echo mb_check_encoding( $str, "utf-8" ) . PHP_EOL;  //輸出空
echo mb_check_encoding( $str, "gbk" ) . PHP_EOL; //輸出1

/**utf-8編碼的字符串  --> 設置文件編碼為utf-8*/
$str = "博客園和github。";
echo mb_check_encoding( $str, "utf-8" ) . PHP_EOL;  //1
echo mb_check_encoding( $str, "gbk" ) . PHP_EOL; //輸出空

$utf8Str = "我abc是誰.";
echo mb_check_encoding( $utf8Str, "utf-8" ) . PHP_EOL;  //輸出1
//如果有中文標點符號則為空!??!
echo mb_check_encoding( $utf8Str, "gbk" ) . PHP_EOL; //輸出1

/**自定義檢測字符串編碼是否為utf-8*/
function is_utf8( $str ) {
    return (bool) preg_match( "http://u", serialize($str) );
}

echo "hello 中國!" .is_utf8( "hello 中國!" ) . PHP_EOL; //1

function check_utf8( $str ) {
    $len = strlen( $str );
    for ( $i = 0; $i < $len; $i ++ ) {
        $c = ord( $str[ $i ] );
        if ( $c > 128 ) {
            if ( ( $c > 247 ) ) {
                return false;
            } elseif ( $c > 239 ) {
                $bytes = 4;
            } elseif ( $c > 223 ) {
                $bytes = 3;
            } elseif ( $c > 191 ) {
                $bytes = 2;
            } else {
                return false;
            }
            if ( ( $i + $bytes ) > $len ) {
                return false;
            }
            while ( $bytes > 1 ) {
                $i ++;
                $b = ord( $str[ $i ] );
                if ( $b < 128 || $b > 191 ) {
                    return false;
                }
                $bytes --;
            }
        }
    }
    
    return true;
} // end of check_utf8

echo check_utf8("hello 中國").PHP_EOL; // 1
echo check_utf8( "x00xE3").PHP_EOL;  //空

/** check a strings encoded value */
function checkEncoding( $string, $string_encoding ) {
    $fs = $string_encoding == "UTF-8" ? "UTF-32" : $string_encoding;
    $ts = $string_encoding == "UTF-32" ? "UTF-8" : $string_encoding;
    
    return $string === mb_convert_encoding( mb_convert_encoding( $string, $fs, $ts ), $ts, $fs );
}

/* test 1 variables */
$string   = "x00x81";
$encoding = "Shift_JIS";

/* test 1 mb_check_encoding (test for bad byte stream) */
if ( true === mb_check_encoding( $string, $encoding ) ) {
    echo "valid (" . $encoding . ") encoded byte stream!" . PHP_EOL;
} else {
    echo "invalid (" . $encoding . ") encoded byte stream!" . PHP_EOL;
}

/* test 1 checkEncoding (test for bad byte sequence(s)) */
if ( true === checkEncoding( $string, $encoding ) ) {
    echo "valid (" . $encoding . ") encoded byte sequence!" . PHP_EOL;
} else {
    echo "invalid (" . $encoding . ") encoded byte sequence!" . PHP_EOL;
}

/* test 2 */
/* test 2 variables */
$string   = "x00xE3";
$encoding = "UTF-8";
/* test 2 mb_check_encoding (test for bad byte stream) */
if ( true === mb_check_encoding( $string, $encoding ) ) {
    echo "valid (" . $encoding . ") encoded byte stream!" . PHP_EOL;
} else {
    echo "invalid (" . $encoding . ") encoded byte stream!" . PHP_EOL;
}

/* test 2 checkEncoding (test for bad byte sequence(s)) */
if ( true === checkEncoding( $string, $encoding ) ) {
    echo "valid (" . $encoding . ") encoded byte sequence!" . PHP_EOL;
} else {
    echo "invalid (" . $encoding . ") encoded byte sequence!" . PHP_EOL;
}

文章參考

http://php.net/manual/zh/func...

轉載注明出處

文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規(guī)行為,您可以聯系管理員刪除。

轉載請注明本文地址:http://m.hztianpu.com/yun/28179.html

相關文章

  • PHP面試???em>之會話控制

    摘要:一的會話也稱為。如果啟動會話成功,則函數返回,否則返回。會話啟動后就可以載入該會話已經注冊的會話變量以便使用。但數組創(chuàng)建的在會話結束后就會失效。預告本周三更新面試??贾W絡協(xié)議,敬請期待。 你好,是我琉憶,歡迎您來到PHP面試專欄。本周(2019.2-25至3-1)的一三五更新的文章如下: 周一:PHP面試??贾畷捒刂浦苋篜HP面試??贾W絡協(xié)議周五:PHP面試??碱}之會話控制和...

    lsxiao 評論0 收藏0
  • PHP面試???em>之設計模式——策略模式

    摘要:策略模式介紹策略模式定義了一系列的算法,并將每一個算法封裝起來,而且使它們還可以相互替換。策略模式讓算法獨立于使用它的客戶而獨立變化。使用策略模式的好處策略模式提供了管理相關的算法族的辦法。使用策略模式可以避免使用多重條件轉移語句。 你好,是我琉憶,PHP程序員面試筆試系列圖書的作者。 本周(2019.3.11至3.15)的一三五更新的文章如下: 周一:PHP面試??贾O計模式——工...

    Drinkey 評論0 收藏0
  • PHP代碼修正CodeSniffer

    摘要:它包含兩類腳本,和地址腳本對文件定義了一系列的代碼規(guī)范通常使用官方的代碼規(guī)范標準,比如的,能夠檢測出不符合代碼規(guī)范的代碼并發(fā)出警告或報錯可設置報錯等級。腳本能自動修正代碼格式上不符合規(guī)范的部分。 Last-Modified: 2019年5月10日13:59:27 參考鏈接 PHP開發(fā)規(guī)范之使用phpcbf腳本自動修正代碼格式 在PhpStorm中使用PSR2編碼規(guī)范phpcbf腳本自...

    khs1994 評論0 收藏0
  • php設計模式

    摘要:我們今天也來做一個萬能遙控器設計模式適配器模式將一個類的接口轉換成客戶希望的另外一個接口。今天要介紹的仍然是創(chuàng)建型設計模式的一種建造者模式。設計模式的理論知識固然重要,但 計算機程序的思維邏輯 (54) - 剖析 Collections - 設計模式 上節(jié)我們提到,類 Collections 中大概有兩類功能,第一類是對容器接口對象進行操作,第二類是返回一個容器接口對象,上節(jié)我們介紹了...

    Dionysus_go 評論0 收藏0

發(fā)表評論

0條評論

tracymac7

|高級講師

TA的文章

閱讀更多
最新活動
閱讀需要支付1元查看
<