php判断是否为中文正则表达式大全(转)
转载自?? http://www.cnblogs.com/DavidYan/articles/2032115.html
?
?
php判断是否为中文正则表达式大全
?
$str="aaa";
if(!eregi("[^\x80-\xff]","$str"))
{
echo "是";
}
else
{
echo "不是";
}
?>
$str = "中国";
echo $str;
echo "
";
//if (preg_match("/^[".chr(0xa1)."-".chr(0xff)."]+$/", $str)) {
//只能在GB2312情况下使用
if (preg_match("/^[\x7f-\xff]+$/", $str)) { //兼容gb2312,utf-8
echo "正确输入";
} else {
echo "错误输入";
}
?>
如果想判断一个字符串内是否有含有中文,请用下面的代码:
if (preg_match("/[\x7f-\xff]/", $string)) {
echo "有中文";
}else{
echo "没有中文";
}
判断中文和编码有关 gbk是双字节,utf8是三字节,可以根据中文的范围来判断???
?
编码范围1. GBK
(GB2312/GB18030)???
\x00-\xff
GBK双字节编码范围???
\x20-\x7f
ASCII???
\xa1-\xff
中文???
\x80-\xff 中文??
??
2. UTF-8
(Unicode)???
\u4e00-\u9fa5
(中文)???
\x3130-\x318F
(韩文???
\xAC00-\xD7A3
(韩文)???
\u0800-\u4e00
(日文)???
ps:
韩文是大于[\u9fa5]的字符???
正则例子:???
preg_replace(”/([\x80-\xff])/”,”",$str);??
preg_replace(”/([u4e00-u9fa5])/”,”",$str);???
?
?
- //判断内容里有没有中文-GBK?(PHP)????? ??
- function ?check_is_chinese( $s ){????? ??
- ????? return ?preg_match( '/[\x80-\xff]./' ,? $s );????? ??
- }????? ??
- ??? ??
- //获取字符串长度-GBK?(PHP)????? ??
- function ?gb_strlen( $str ){????? ??
- ????? $count ?=?0;????? ??
- ????? for ( $i =0;? $i strlen ( $str );? $i ++){????? ??
- ????????? $s ?=? substr ( $str ,? $i ,?1);????? ??
- ????????? if ?(preg_match( "/[\x80-\xff]/" ,? $s ))?++ $i ;????? ??
- ???????????++ $count ;????? ??
- ?????}????? ??
- ????? return ? $count ;????? ??
- }????? ??
- ??? ??
- //截取字符串字串-GBK?(PHP)????? ??
- function ?gb_substr( $str ,? $len ){????? ??
- ????? $count ?=?0;????? ??
- ????? for ( $i =0;? $i strlen ( $str );? $i ++){????? ??
- ????????? if ( $count ?==? $len )? break ;????? ??
- ????????? if (preg_match( "/[\x80-\xff]/" ,? substr ( $str ,? $i ,?1)))?++ $i ;????? ??
- ???????????++ $count ;????????????? ??
- ?????}????? ??
- ????? return ? substr ( $str ,?0,? $i );????? ??
- }????? ??
- ??? ??
- //统计字符串长度-UTF8?(PHP)????? ??
- function ?utf8_strlen( $str )?{????? ??
- ????? $count ?=?0;????? ??
- ????? for ( $i ?=?0;? $i ? strlen ( $str );? $i ++){????? ??
- ????????? $value ?=?ord( $str [ $i ]);????? ??
- ????????? if ( $value ?>?127)?{????? ??
- ????????????? $count ++;????? ??
- ????????????? if ( $value ?>=?192?&&? $value ? $i ++;????? ??
- ????????????? elseif ( $value ?>=?224?&&? $value ? $i ?=? $i ?+?2;????? ??
- ????????????? elseif ( $value ?>=?240?&&? $value ? $i ?=? $i ?+?3;????? ??
- ????????????? else ? die ( 'Not?a?UTF-8?compatible?string' );????? ??
- ?????????}????? ??
- ????????? $count ++;????? ??
- ?????}????? ??
- ????? return ? $count ;????? ??
- }????? ??
- ??? ??
- ??? ??
- //截取字符串-UTF8(PHP)????? ??
- function ?utf8_substr( $str , $position , $length ){????? ??
- ????? $start_position ?=? strlen ( $str );????? ??
- ????? $start_byte ?=?0;????? ??
- ????? $end_position ?=? strlen ( $str );????? ??
- ????? $count ?=?0;????? ??
- ????? for ( $i ?=?0;? $i ? strlen ( $str );? $i ++){????? ??
- ????????? if ( $count ?>=? $position ?&&? $start_position ?>? $i ){????? ??
- ????????????? $start_position ?=? $i ;????? ??
- ????????????? $start_byte ?=? $count ;????? ??
- ?????????}????? ??
- ????????? if (( $count - $start_byte )>= $length )?{????? ??
- ????????????? $end_position ?=? $i ;????? ??
- ????????????? break ;????? ??
- ?????????}????????? ??
- ????????? $value ?=?ord( $str [ $i ]);????? ??
- ????????? if ( $value ?>?127){????? ??
- ????????????? $count ++;????? ??
- ????????????? if ( $value ?>=?192?&&? $value ? $i ++;????? ??
- ????????????? elseif ( $value ?>=?224?&&? $value ? $i ?=? $i ?+?2;????? ??
- ????????????? elseif ( $value ?>=?240?&&? $value ? $i ?=? $i ?+?3;????? ??
- ????????????? else ? die ( 'Not?a?UTF-8?compatible?string' );????? ??
- ?????????}????? ??
- ????????? $count ++;????? ??
- ??? ??
- ?????}????? ??
- ????? return ( substr ( $str , $start_position , $end_position - $start_position ));????? ??
- }????? ??
- ??? ??
- //判断是否是有韩文-UTF-8?(JavaScript)????? ??
- function ?checkKoreaChar(str)?{????? ??
-
?????
for
(i=0;?i
- ????????? if (((str.charCodeAt(i)?>?0x3130?&&?str.charCodeAt(i)?0x318F)?||?(str.charCodeAt(i)?>=?0xAC00?&&?str.charCodeAt(i)?
- ????????????? return ?true;????? ??
- ?????????}????? ??
- ?????}????? ??
- ????? return ?false;????? ??
- }????? ??
- ??? ??
- //判断是否有中文字符-GBK?(JavaScript)????? ??
- function ?check_chinese_char(s){????? ??
- ????? return ?(s.length?!=?s.replace(/[^\x00-\xff]/g, "**" ).length);????? ??
- }???
?
UTF-8匹配:
在javascript中,要判断字符串是中文是很简单的。比如:
var str = "php编程";
if (/^[\u4e00-\u9fa5]+$/.test(str)) {
alert("该字符串全部是中文");
}
else{
alert("该字符串不全部是中文");
}
php中,是用\x表示十六进制数据的。于是,变换成如下的代码:
$str = "php编程";
if (preg_match("/^[\x4e00-\x9fa5]+$/",$str)) {
print("该字符串全部是中文");
} else {
print("该字符串不全部是中文");
}
貌似不报错了,判断的结果也正确,不过把$str换成“编程”两字,结果却还是显示“该字符串不全部是中文”,看来这样的判断还是不够准确。
重要:查阅了发现,对于[\x4e00-\x9fa5]这块东西,自己做一个强化的解释
php的正则中, [\x4e00-\x9fa5],其实就是 字符和字符组的概念, \x{hex},表达一个16进制数, 需要注意的是hex 可以是1-2位的,也可以是4位的,但是如果是4位的必须加上大括号,
同时,如果是大于x{FF}的hex,必须和u 修饰符连用,不然会非法出错
网上只能找到匹配全角字符的正则:??
^[\x80-\xff]*^/???
,这里可以不加大括号
[\u4e00-\u9fa5]可以匹配中文,但是PHP又不支持???
不过,既然\x表示的十六进制数据,为什么和js里边提供的范围\x4e00-\x9fa5不一样呢?于是我就换成了下边的代码,发现真的准确了:
$str = "php编程";
if (preg_match("/^[\x{4e00}-\x{9fa5}]+$/u",$str)) {
print("该字符串全部是中文");
} else {
print("该字符串不全部是中文");
}
知道了php中utf-8编码下用正则表达式匹配汉字的最终正确表达式――/^[\x{4e00}-\x{9fa5}]+$/u,
参考以上文章写了如下一段测试代码(复制以下代码保存成.php文件)
$action = trim($_GET['action']);
if($action == "sub")
{
??? $str =
$_POST['dir'];???
???
//if(!preg_match("/^[".chr(0xa1)."-".chr(0xff)."A-Za-z0-9_]+$/",$str))
//GB2312汉字字母数字下划线正则表达式
???
if(!preg_match("/^[\x{4e00}-\x{9fa5}A-Za-z0-9_]+$/u",$str))??
//UTF-8汉字字母数字下划线正则表达式
???
{??
???????
echo "您输入的[".$str."]含有违法字符";??
??? }
??? else
??? {
???????
echo "您输入的[".$str."]完全合法,通过!";??
??? }
}
?>
GBK:
preg_match("/^[".chr(0xa1)."-".chr(0xff)."A-Za-z0-9_]+$/",$str); //GB2312汉字字母数字下划线正则表达式
?
$str="aaa";
if(!eregi("[^\x80-\xff]","$str"))
{
echo "是";
}
else
{
echo "不是";
}
?>
$str = "中国";
echo $str;
echo "
";
//if (preg_match("/^[".chr(0xa1)."-".chr(0xff)."]+$/", $str)) {
//只能在GB2312情况下使用
if (preg_match("/^[\x7f-\xff]+$/", $str)) { //兼容gb2312,utf-8
echo "正确输入";
} else {
echo "错误输入";
}
?>
如果想判断一个字符串内是否有含有中文,请用下面的代码:
if (preg_match("/[\x7f-\xff]/", $string)) {
echo "有中文";
}else{
echo "没有中文";
}
?
?
?
?
?

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

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

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

SublimeText3 Linux new version
SublimeText3 Linux latest version

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Zend Studio 13.0.1
Powerful PHP integrated development environment

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.