本文主要和大家分享mb_check_encoding使用方法,希望能帮助到大家。
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_convert_encoding与iconv函数
以上是PHP之mb_check_encoding使用方法分享的详细内容。更多信息请关注PHP中文网其他相关文章!

PHP在现代Web开发中仍然重要,尤其在内容管理和电子商务平台。1)PHP拥有丰富的生态系统和强大框架支持,如Laravel和Symfony。2)性能优化可通过OPcache和Nginx实现。3)PHP8.0引入JIT编译器,提升性能。4)云原生应用通过Docker和Kubernetes部署,提高灵活性和可扩展性。

PHP适合web开发,特别是在快速开发和处理动态内容方面表现出色,但不擅长数据科学和企业级应用。与Python相比,PHP在web开发中更具优势,但在数据科学领域不如Python;与Java相比,PHP在企业级应用中表现较差,但在web开发中更灵活;与JavaScript相比,PHP在后端开发中更简洁,但在前端开发中不如JavaScript。

PHP和Python各有优势,适合不同场景。1.PHP适用于web开发,提供内置web服务器和丰富函数库。2.Python适合数据科学和机器学习,语法简洁且有强大标准库。选择时应根据项目需求决定。

PHP是一种广泛应用于服务器端的脚本语言,特别适合web开发。1.PHP可以嵌入HTML,处理HTTP请求和响应,支持多种数据库。2.PHP用于生成动态网页内容,处理表单数据,访问数据库等,具有强大的社区支持和开源资源。3.PHP是解释型语言,执行过程包括词法分析、语法分析、编译和执行。4.PHP可以与MySQL结合用于用户注册系统等高级应用。5.调试PHP时,可使用error_reporting()和var_dump()等函数。6.优化PHP代码可通过缓存机制、优化数据库查询和使用内置函数。7

PHP成为许多网站首选技术栈的原因包括其易用性、强大社区支持和广泛应用。1)易于学习和使用,适合初学者。2)拥有庞大的开发者社区,资源丰富。3)广泛应用于WordPress、Drupal等平台。4)与Web服务器紧密集成,简化开发部署。

PHP在现代编程中仍然是一个强大且广泛使用的工具,尤其在web开发领域。1)PHP易用且与数据库集成无缝,是许多开发者的首选。2)它支持动态内容生成和面向对象编程,适合快速创建和维护网站。3)PHP的性能可以通过缓存和优化数据库查询来提升,其广泛的社区和丰富生态系统使其在当今技术栈中仍具重要地位。

在PHP中,弱引用是通过WeakReference类实现的,不会阻止垃圾回收器回收对象。弱引用适用于缓存系统和事件监听器等场景,需注意其不能保证对象存活,且垃圾回收可能延迟。

\_\_invoke方法允许对象像函数一样被调用。1.定义\_\_invoke方法使对象可被调用。2.使用$obj(...)语法时,PHP会执行\_\_invoke方法。3.适用于日志记录和计算器等场景,提高代码灵活性和可读性。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。

DVWA
Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

SublimeText3 英文版
推荐:为Win版本,支持代码提示!

记事本++7.3.1
好用且免费的代码编辑器

Atom编辑器mac版下载
最流行的的开源编辑器