php判断是不是为中文正则表达式大全(转)
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 "没有中文";
}
?
?
?
?
?

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

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics



The Count function is used to count the number of numbers in a specified range. It ignores text, logical values, and null values, but counts empty cells. The Count function only counts the number of cells that contain actual numbers. The CountA function is used to count the number of non-empty cells in a specified range. It not only counts cells containing actual numbers, but also counts the number of non-empty cells containing text, logical values, and formulas.

In today's era of rapid technological development, programming languages are springing up like mushrooms after a rain. One of the languages that has attracted much attention is the Go language, which is loved by many developers for its simplicity, efficiency, concurrency safety and other features. The Go language is known for its strong ecosystem with many excellent open source projects. This article will introduce five selected Go language open source projects and lead readers to explore the world of Go language open source projects. KubernetesKubernetes is an open source container orchestration engine for automated

"Go Language Development Essentials: 5 Popular Framework Recommendations" As a fast and efficient programming language, Go language is favored by more and more developers. In order to improve development efficiency and optimize code structure, many developers choose to use frameworks to quickly build applications. In the world of Go language, there are many excellent frameworks to choose from. This article will introduce 5 popular Go language frameworks and provide specific code examples to help readers better understand and use these frameworks. 1.GinGin is a lightweight web framework with fast

Laravel is a popular PHP framework that is highly scalable and efficient. It provides many powerful tools and libraries that allow developers to quickly build high-quality web applications. Among them, LaravelEcho and Pusher are two very important tools through which WebSockets communication can be easily implemented. This article will detail how to use these two tools in Laravel applications. What are WebSockets? WebSockets

With the development of the Internet and the advancement of information technology, the era of big data has arrived, and fields such as data analysis and machine learning have also been widely used. In these fields, task scheduling is an inevitable problem. How to achieve efficient task scheduling is crucial to improving efficiency. In this article, we will introduce how to use Golang's web framework Echo framework to implement distributed task scheduling. 1. Introduction to the Echo framework Echo is a high-performance, scalable, lightweight GoWeb framework. It is based on HTTP

Detailed explanation of the role and usage of the echo keyword in PHP PHP is a widely used server-side scripting language, which is widely used in web development. The echo keyword is a method used to output content in PHP. This article will introduce in detail the function and use of the echo keyword. Function: The main function of the echo keyword is to output content to the browser. In web development, we need to dynamically present data to the front-end page. At this time, we can use the echo keyword to output the data to the page. e

As a fast and efficient programming language, Go language has always been favored by programmers. In the Go language ecosystem, frameworks play a vital role in helping developers build applications faster. This article will introduce five Go language frameworks to let you understand their characteristics and usage. 1. Gin framework The Gin framework is a lightweight Web framework with fast and high performance characteristics. Use the Gin framework to quickly build RESTful APIs and web applications. Here is a simple example code:

The most popular Go frameworks at present are: Gin: lightweight, high-performance web framework, simple and easy to use. Echo: A fast, highly customizable web framework that provides high-performance routing and middleware. GorillaMux: A fast and flexible multiplexer that provides advanced routing configuration options. Fiber: A performance-optimized, high-performance web framework that handles high concurrent requests. Martini: A modular web framework with object-oriented design that provides a rich feature set.
