上篇 http://www.BkJia.com/kf/201202/118458.html
//正則表達式的技術知識
// []定義字符集和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;
摘自 kaituozhe345的專欄