资讯专栏INFORMATION COLUMN

PHP之mb_check_encoding使用

tracymac7 / 2272人阅读

摘要:检查字符串在指定的编码里是否有效检查指定的字节流在指定的编码里是否有效。它能有效避免所谓的无效编码攻击。要检查的字节流。成功时返回,或者在失败时返回。输出空输出编码的字符串设置文件编码为博客园和。

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".

// 检查指定的字节流在指定的编码里是否有效。它能有效避免所谓的“无效编码攻击(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.

要检查的字节流。如果省略了这个参数,此函数会检查所有来自最初请求所有的输入。

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( "//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...

转载注明出处

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/28179.html

相关文章

  • PHP面试常考会话控制

    摘要:一的会话也称为。如果启动会话成功,则函数返回,否则返回。会话启动后就可以载入该会话已经注册的会话变量以便使用。但数组创建的在会话结束后就会失效。预告本周三更新面试常考之网络协议,敬请期待。 你好,是我琉忆,欢迎您来到PHP面试专栏。本周(2019.2-25至3-1)的一三五更新的文章如下: 周一:PHP面试常考之会话控制周三:PHP面试常考之网络协议周五:PHP面试常考题之会话控制和...

    lsxiao 评论0 收藏0
  • PHP面试常考设计模式——策略模式

    摘要:策略模式介绍策略模式定义了一系列的算法,并将每一个算法封装起来,而且使它们还可以相互替换。策略模式让算法独立于使用它的客户而独立变化。使用策略模式的好处策略模式提供了管理相关的算法族的办法。使用策略模式可以避免使用多重条件转移语句。 你好,是我琉忆,PHP程序员面试笔试系列图书的作者。 本周(2019.3.11至3.15)的一三五更新的文章如下: 周一:PHP面试常考之设计模式——工...

    Drinkey 评论0 收藏0
  • PHP代码修正CodeSniffer

    摘要:它包含两类脚本,和地址脚本对文件定义了一系列的代码规范通常使用官方的代码规范标准,比如的,能够检测出不符合代码规范的代码并发出警告或报错可设置报错等级。脚本能自动修正代码格式上不符合规范的部分。 Last-Modified: 2019年5月10日13:59:27 参考链接 PHP开发规范之使用phpcbf脚本自动修正代码格式 在PhpStorm中使用PSR2编码规范phpcbf脚本自...

    khs1994 评论0 收藏0
  • php设计模式

    摘要:我们今天也来做一个万能遥控器设计模式适配器模式将一个类的接口转换成客户希望的另外一个接口。今天要介绍的仍然是创建型设计模式的一种建造者模式。设计模式的理论知识固然重要,但 计算机程序的思维逻辑 (54) - 剖析 Collections - 设计模式 上节我们提到,类 Collections 中大概有两类功能,第一类是对容器接口对象进行操作,第二类是返回一个容器接口对象,上节我们介绍了...

    Dionysus_go 评论0 收藏0

发表评论

0条评论

tracymac7

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<