PHP判断字符串是纯英文、纯汉字或汉英混合
PHP判断字符串是否为中文(或英文)的方法,除了正则表达式判断和拆分字符判断字符的值是否小于128
?
外还有一种比较特别的方法。
使用php中的mb_strlen和strlen函数判断
方法比较简单:分别使用以上两个函数以当前编码测出字符的返回值,然后比较返回值。
返回值相等的为纯英文、纯数字、英数混排;
返回值不等,且strlen返回值可被mb_strlen整除的为纯汉字
返回值不等,且strlen返回值不可被mb_strlen整除的为英汉或数汉混排
?
看一下以下的例子:
<?php $strarray[1] = "hello";$strarray[2] = "123456";$strarray[3] = "123hello"; $strarray[4] = "你好";$strarray[5] = "123你好";$strarray[6] = "hello你好";$strarray[7] = "123hello你好"; foreach ($strarray as $key->$value) { $x = mb_strlen($value,'gb2312'); $y = strlen($value); echo $strarray[$key].' <span style="color: #ff0000;">'.$x.'</span> <span style="color:#ff0000;">'.$y.'</span>'; } ?>?
运行后的结果是:
hello 5 5
123456 6 6
123hello 8 8
你好 2 4
123你好 5 7
hello你好 7 9
123hello你好 10 12
?
来源: http://007blogchina.appspot.com/?p=130001
?
HP没有直接函数来判断一个字符串是否是纯英文或纯汉字以及汉英混合,只能自己写函数。要想实现此功能就必需对字符集汉字编码占位进行了解,就目前国内比较常用的字符集当属UTF8与GBK了。
UTF8每个汉字等于3个长度;
GBK每个汉字等于2个长度;
利用以上汉字与英文的差异,我们就可以利用mb_strlen函数与strlen函数分别计算出两组长度数字,然后根据规律进行运算即可判断出字符串的类型了。
?
UTF-8实例
<?php/** * PHP判断字符串纯汉字 OR 纯英文 OR 汉英混合 */echo '<meta charset="utf-8" />';function utf8_str($str){ $mb = mb_strlen($str,'utf-8'); $st = strlen($str); if($st==$mb) return '纯英文'; if($st%$mb==0 && $st%3==0) return '纯汉字'; return '汉英混合';} $str = '博客';echo '字符串:<span style="color:red">'.$str.'</span>,是<span style="color:red">'.utf8_str($str).'</span>';?>?
GBK方法
function gbk_str($str){ $mb = mb_strlen($str,'gbk'); $st = strlen($str); if($st==$mb) return '纯英文'; if($st%$mb==0 && $st%2==0) return '纯汉字'; return '汉英混合';}?
来源: http://www.qttc.net/201207142.html
?
?
?

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl


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

Dreamweaver Mac version
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

WebStorm Mac version
Useful JavaScript development tools

Atom editor mac version download
The most popular open source editor

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software
