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
?
?
?

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version
Useful JavaScript development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.