日期:2014-05-17  浏览次数:20498 次

php知识点复习之正则表达式

//正则表达式的技术知识


// []定义字符集和 example [a-z] [A-Z] [0-9] [\f\r\n\t]


//定位符^[A-Z][0-9]$开头结尾


//量词 * + ? {n} {m,n} {n.}指的都是重复次数


//选择 |


//可打印字符:ASCII中33-127表示的字符,就是我们看得见的符号 空格,删除,回车,取消等等就典型的是不可打印字符


//POSOX正则表达式
//常用函数
echo "<br>";
$b4 = "abc";
$pattern = "[[:alpha:]]";
echo ereg($pattern,$b4);
//输出1
//eregi() 无符号的


$b7 = "asd&ass@ass&adfdf";

$pattern1 = "&";
$pattern2 = "[&@]";
print_r(split($pattern1,$b7));
print_r(split($pattern2,$b7));


echo "<br>";
$b8 = "helloworld";
$pattern3 = "world";
$replacement1 = "persion";
echo ereg_replace($pattern3,$replacement1,$b8);


echo "<br>";
echo sql_regcase("abcdefg");
//输出:[Aa][Bb][Cc][Dd][Ee][Ff][Gg]


//Perl 正则表达式 perl是在支持posix的基础上额外的扩展 \d数字 \D非数字 \s空白 \S非空白 \w字母数学符号下划线 \W非字母数字符号下划线

//常用函数array preg_grep ( string pattern, array input )
echo "<br>";
$w1 = array("adad","adad4","asda","1asf3","sdfs");
$fl_array = preg_grep ("/^\D{1,}$/", $w1);//此处强调一点{1,}而不是posix中的{1.}
print_r($fl_array);


echo "<br>";
echo preg_match ("/a/","abc");
//输出1


echo "<br>";
//\b表示单词的边界
if (preg_match ("/\bweb\b/i", "PHP is the web scripting language of choice.")) {
print "A match was found.";
} else {
print "A match was not found.";
}


if (preg_match ("/\bweb\b/i", "PHP is the website scripting language of choice.")) {
print "A match was found.";
} else {
print "A match was not found.";
}
//尤其是在匹配电子邮箱是常用


echo "<br>";
$pattern5 = "/[\*]+/";
$w3 = "aaa***bbb*ccc";
print_r(preg_split($pattern5,$w3));


echo "<br>";
$pattern6 = "/world/";
$content2 = "girl";
$tre = "hello world";
echo preg_replace($pattern6,$content2,$tre);


//string preg_quote ( string str [, string delimiter] )转义
echo "<br>";
$keywords = "$40 for a g3/400";
$keywords = preg_quote ($keywords, "/");//此处的/若是不加,就不会转义g3/400中的/;
echo $keywords;