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

<?php
/**
* Created by PhpStorm.
* User: zhangrongxiang
* Date: 2018/1/27
* Time: 下午2:59
*/ /**纯数字和英文字母组合*/
$utf8Str = "I have 4 books and 2 magazines to check out. ";
echo ( mb_check_encoding( $utf8Str, 'utf-8' ) ) . PHP_EOL; //输出1
echo ( mb_check_encoding( $utf8Str, 'gbk' ) ) . PHP_EOL; //输出1
echo bin2hex( $utf8Str ) . PHP_EOL;
//492068617665203420626f6f6b7320616e642032206d6167617a696e657320746f20636865636b206f75742e20
$gbkStr = mb_convert_encoding( $utf8Str, 'gbk', 'utf-8' );
echo bin2hex( $gbkStr ) . PHP_EOL;
//492068617665203420626f6f6b7320616e642032206d6167617a696e657320746f20636865636b206f75742e20 /**gbk编码的字符串 --> 设置文件编码为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( "\x00\xE3").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 = "\x00\x81";
$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 = "\x00\xE3";
$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;
}

文章参考

转载注明出处

PHP之mb_check_encoding使用的更多相关文章

  1. PHP7函数大全(4553个函数)

    转载来自: http://www.infocool.net/kb/PHP/201607/168683.html a 函数 说明 abs 绝对值 acos 反余弦 acosh 反双曲余弦 addcsla ...

  2. php中文字符串翻转

    转自:http://www.oschina.net/code/snippet_613962_17070 <?php header("content-type:text/html;cha ...

  3. PHP用substr截取字符串出现中文乱码问题用mb_substr

    PHP用substr截取字符串出现中文乱码问题用mb_substr实例:mb_substr('截取中文乱码问题测试',0,5, 'utf-8'); 语法 : string substr (string ...

  4. PHP5 GD库生成图形验证码(汉字)

    PHP5 GD库生成图形验证码且带有汉字的实例分享. 1,利用GD库函数生成图片,并在图片上写指定字符imagecreatetruecolor 新建一个真彩色图像imagecolorallocate ...

  5. php中文字符串反转

    <?php header("content-type:text/html;charset=utf-8"); /** 此函数的作用是反转中文字符串 mb_strlen() 获取 ...

  6. PHP5生成图形验证码(有汉字)

    利用PHP5中GD库生成图形验证码 类似于下面这样 1.利用GD库函数生成图片,并在图片上写指定字符 imagecreatetruecolor   新建一个真彩色图像      imagecolora ...

  7. php 中文转拼音首字母问题

    <?php /* 中文汉字转拼音首字母的PHP简易实现方法. 要求: 只能是GB2312码表里面中文字符 转换得到字符串对应的拼音首字母大写. 用法: echo zh2py::conv('Chi ...

  8. PHP编码相关函数试题

    1.检查字符串在指定的编码里是否有效的函数是什么? 2.获取字符编码的函数是什么? 3.解析 GET/POST/COOKIE 数据并设置全局变量的函数是什么? 4.大小写不敏感地查找字符串在另一个字符 ...

  9. PHP截取带有汉字的字符串,将汉字按两个字节计算

    <?php header("Content-type:text/html;charset=utf-8"); /** *截取字符串,汉字占两个字节,字母占一个字节 *页面编码必 ...

随机推荐

  1. Objective-C 学习笔记(三) Numbers/数字

    Objective-C Numbers/数字 Objective-C中提供了一系列的NSNumber和重要的工作方法: + (NSNumber *)numberWithBool:(BOOL)value ...

  2. java 封装返回json数据

    做的东西,一直是用easyui的,和后台的交互数据都是json格式的. 今天想要单独弄一个json数据返回给前台,其实是比较简单的问题,json接触不多,记录一下. 代码: public static ...

  3. ubuntu16.04系统精简

    ubuntu16.04系统精简 一.更新系统 apt-get update apt-get dist-upgrade 二.查看所有内核 sudo dpkg --get-selections |grep ...

  4. LINQ to Entities 查询中的标准查询运算符

    投影和筛选方法 投影指的是转换的结果集到所需的窗体中的元素. 例如,可以从结果集中的每个对象投影所需的属性子集,可以投影一个属性并对其执行数学计算,也可以从结果集投影整个对象. 投影方法有 Selec ...

  5. 基于Quartz.net的远程任务管理系统-起绪

    Quartz.net这一个任务调度框架,相信大部分的开发者都非常的熟悉了. 往往在一个项目之中,我们会有很多的定时任务,加之多人参与编码,难免会有些难于管理等问题.为统一编写规范,以及对定时任务的管理 ...

  6. 开源一款强大的文件服务组件(QJ_FileCenter)(系列三 访问接口与项目集成)

    系列文章 1. 开源一款强大的文件服务组件(QJ_FileCenter)(系列一) 2. 开源一款强大的文件服务组件(QJ_FileCenter)(系列二 安装说明) 3. 开源一款强大的文件服务组件 ...

  7. JAVA—编码问题

    一.编码.(引用  百度百科) 编码是信息从一种形式或格式转换为另一种形式的过程也称为计算机编程语言的代码简称编码.用预先规定的方法将文字.数字或其它对象编成数码,或将信息.数据转换成规定的电脉冲信号 ...

  8. ES6——异步操作之Promise

    基本概念: Promise : 是 ES6 中新增的异步编程解决方案,提现在代码中他是一个对象 可以通过Promise构造函数来实例化. -new Promise(cb) ===> 实例的基本使 ...

  9. ASP.NET Core使用NLog记录日志

    1.根目录新建nlog.config配置文件 <?xml version="1.0"?> <nlog xmlns="http://www.nlog-pr ...

  10. 【OCP-12c】CUUG 071题库考试原题及答案解析(16)

    16.(7-5) choose the best answerThe PRODUCTS table has the following structure:Evaluate the following ...