mb_strlen函数指定不同的编码输出结果不同,哪位高手大侠给解释下原因
页面编码utf-8
$text = "啊啊啊啊"; echo mb_strlen($text,'utf8')."<br/>";echo mb_strlen($text,'gbk')."<br/>";echo mb_strlen($text,'gb2312')."<br/>";echo strlen($text);
输出:4 6 8 12
页面编码gb2312
$text = "啊啊啊啊"; echo mb_strlen($text,'utf8')."<br/>";echo mb_strlen($text,'gbk')."<br/>";echo mb_strlen($text,'gb2312')."<br/>";echo strlen($text);
输出:4 4 4 8
回复讨论(解决方案)
你指定错误的编码,自然得不到正确的结果
这是PHP官网字符集列表
http://www.php.net/manual/en/mbstring.supported-encodings.php
mb_internal_encoding("UTF-8");
echo mb_internal_encoding();
"啊啊啊啊"的字节16进制表示为
UTF-8: E5 95 8A E5 95 8A E5 95 8A E5 95 8A ---12
GB2312: B0 A1 B0 A1 B0 A1 B0 A1 ---8
在utf-8时
utf-8 [E5 95 8A] [E5 95 8A] [E5 95 8A] [E5 95 8A] --- 4
gbk [E5 95]? [8A E5]? [95 8A]? [E5 95]? [8A E5]? [95 8A]? --- 6
gb2312 [E5 95]? [8A] [E5 95]? [8A] [E5 95]? [8A] [E5 95]? [8A] ---8
注:8A开头不存在于gb2312(最低A1开始),所以独立计算了
在gb2312时
utf-8( 不确定) 由于不存在B0字节开头的utf-8字符,我猜mb是“智能”地按双字节计算 ---4
gbk/gb2312 [B0 A1] [B0 A1] [B0 A1] [B0 A1] ---4
实测结果(php 5.4.12)
utf-8 下得 4 6 8 12
gb2312 下得 8 4 4 8
不需要做任何解释,只有在正确的字符集中才可以得到正确的结果

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.

ThebestpracticesforsendingemailssecurelyinPHPinclude:1)UsingsecureconfigurationswithSMTPandSTARTTLSencryption,2)Validatingandsanitizinginputstopreventinjectionattacks,3)EncryptingsensitivedatawithinemailsusingOpenSSL,4)Properlyhandlingemailheaderstoa

TooptimizePHPapplicationsforperformance,usecaching,databaseoptimization,opcodecaching,andserverconfiguration.1)ImplementcachingwithAPCutoreducedatafetchtimes.2)Optimizedatabasesbyindexing,balancingreadandwriteoperations.3)EnableOPcachetoavoidrecompil

DependencyinjectioninPHPisadesignpatternthatenhancesflexibility,testability,andmaintainabilitybyprovidingexternaldependenciestoclasses.Itallowsforloosecoupling,easiertestingthroughmocking,andmodulardesign,butrequirescarefulstructuringtoavoidover-inje

PHP performance optimization can be achieved through the following steps: 1) use require_once or include_once on the top of the script to reduce the number of file loads; 2) use preprocessing statements and batch processing to reduce the number of database queries; 3) configure OPcache for opcode cache; 4) enable and configure PHP-FPM optimization process management; 5) use CDN to distribute static resources; 6) use Xdebug or Blackfire for code performance analysis; 7) select efficient data structures such as arrays; 8) write modular code for optimization execution.

OpcodecachingsignificantlyimprovesPHPperformancebycachingcompiledcode,reducingserverloadandresponsetimes.1)ItstorescompiledPHPcodeinmemory,bypassingparsingandcompiling.2)UseOPcachebysettingparametersinphp.ini,likememoryconsumptionandscriptlimits.3)Ad


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Notepad++7.3.1
Easy-to-use and free code editor
