php中ereg() ,preg_match() 有啥区别?
来源:互联网 发布:php5.6 mysql不支持 编辑:程序博客网 时间:2024/06/05 05:39
PHP中几个正则函数的用法及区别
函数用法:
preg_match(mode, string subject, array matches); 相比ereg更加规范,执行效率越高
ereg(mode, string subject, array regs);
mode:正则表达式(preg_match中的mode必须以’/'开始和“/”结束)
subject: 需要验证的字符串
matchs/regs: 匹配后得到的结果。以数组的形式存储
preg_match和 preg_match_all区别是preg_match只匹配一次。而preg_match_all全部匹配,直到字符串结束。
示例如下:
<?php
$date = date(‘Y-m-d’);
//ereg函数
ereg("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})",$date,$rs);
var_dump($rs);
unset($rs);
echo "—————————————–<br>";
//preg_match函数
preg_match("/([\d]{4})-([\d]{1,2})-([\d]{1,2})/",$date.$date,$rs);
var_dump($rs);
unset($rs);
echo "—————————————–<br>";
//preg_match_all函数
preg_match_all("/([\d]{4})-([\d]{1,2})-([\d]{1,2})/",$date.$date,$rs);
var_dump($rs);
?>
如下输出:
array(4) {
[0]=>
string(10) "2012-08-20"
[1]=>
string(4) "2012"
[2]=>
string(2) "08"
[3]=>
string(2) "20"
}
—————————————–
array(4) {
[0]=>
string(10) "2012-08-20"
[1]=>
string(4) "2012"
[2]=>
string(2) "08"
[3]=>
string(2) "20"
}
—————————————–
array(4) {
[0]=>
array(2) {
[0]=>
string(10) "2012-08-20"
[1]=>
string(10) "2012-08-20"
}
[1]=>
array(2) {
[0]=>
string(4) "2012"
[1]=>
string(4) "2012"
}
[2]=>
array(2) {
[0]=>
string(2) "08"
[1]=>
string(2) "08"
}
[3]=>
array(2) {
[0]=>
string(2) "20"
[1]=>
string(2) "20"
}
}
PHP中如何用正则函数来验证中文字符串
验证中文字符串正则表达式为: /^[\x{4e00}-\x{9fa5}]+$/u
方法如下:
$str = "个人博客";
if(preg_match("/^[\x{4e00}-\x{9fa5}]+$/u",$str)){
echo ‘皆为中文’;
}else{
echo ‘不完全是中文’;
}
函数用法:
preg_match(mode, string subject, array matches); 相比ereg更加规范,执行效率越高
ereg(mode, string subject, array regs);
mode:正则表达式(preg_match中的mode必须以’/'开始和“/”结束)
subject: 需要验证的字符串
matchs/regs: 匹配后得到的结果。以数组的形式存储
preg_match和 preg_match_all区别是preg_match只匹配一次。而preg_match_all全部匹配,直到字符串结束。
示例如下:
<?php
$date = date(‘Y-m-d’);
//ereg函数
ereg("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})",$date,$rs);
var_dump($rs);
unset($rs);
echo "—————————————–<br>";
//preg_match函数
preg_match("/([\d]{4})-([\d]{1,2})-([\d]{1,2})/",$date.$date,$rs);
var_dump($rs);
unset($rs);
echo "—————————————–<br>";
//preg_match_all函数
preg_match_all("/([\d]{4})-([\d]{1,2})-([\d]{1,2})/",$date.$date,$rs);
var_dump($rs);
?>
如下输出:
array(4) {
[0]=>
string(10) "2012-08-20"
[1]=>
string(4) "2012"
[2]=>
string(2) "08"
[3]=>
string(2) "20"
}
—————————————–
array(4) {
[0]=>
string(10) "2012-08-20"
[1]=>
string(4) "2012"
[2]=>
string(2) "08"
[3]=>
string(2) "20"
}
—————————————–
array(4) {
[0]=>
array(2) {
[0]=>
string(10) "2012-08-20"
[1]=>
string(10) "2012-08-20"
}
[1]=>
array(2) {
[0]=>
string(4) "2012"
[1]=>
string(4) "2012"
}
[2]=>
array(2) {
[0]=>
string(2) "08"
[1]=>
string(2) "08"
}
[3]=>
array(2) {
[0]=>
string(2) "20"
[1]=>
string(2) "20"
}
}
PHP中如何用正则函数来验证中文字符串
验证中文字符串正则表达式为: /^[\x{4e00}-\x{9fa5}]+$/u
方法如下:
$str = "个人博客";
if(preg_match("/^[\x{4e00}-\x{9fa5}]+$/u",$str)){
echo ‘皆为中文’;
}else{
echo ‘不完全是中文’;
}
0 0
- php中ereg() ,preg_match() 有啥区别?
- PHP中ereg与preg_match和preg_matc…
- php中preg_match和preg_match_all的区别
- php中preg_match()和preg_match_all()的区别
- PHP中ereg 和ereg _replace的配合
- PHP函数ereg与preg的区别
- PHP ereg 和 preg 的区别
- 正则表达式 preg_match()与ereg()函数
- php中ereg()函数的用法
- PHP preg_match()
- PHP中preg_match正则表达式的使用
- 注意PHP正则用法中ereg的使用
- PHP中使用正则表达式详解 preg_match() preg_replace() preg_matchall()
- PHP preg_match函数
- PHP preg_match 应用
- PHP preg_match 用法
- php preg_match用法介绍
- php preg_match的使用
- Servlet简介
- 本地上传文件大小不受限制,但发布到服务器只能传1M以内的
- batch downloading file in python
- 哪里跌倒哪里爬起来 总结下STL的内部实现和特点
- Mac 配置为https服务器
- php中ereg() ,preg_match() 有啥区别?
- Ehcache 整合Spring 使用页面、对象缓存
- 动态规划C++::杭电ACM1003
- HTTP协议中PUT和POST使用区别
- JavaScript事件机制详解
- Java制作证书的工具keytool用法总结
- BM算法详解
- 直方图中bins应如何理解及处理
- 图像金字塔