1.preg_match()
函數(shù)原型:int preg_match (string $pattern, string $content [, array $matches])
preg_match ()函數(shù)在$content字符串中搜索與$pattern給出的正則表達式相匹配的內(nèi)容。如果提供了$matches,則將匹配結(jié)果放入其 中。$matches[0]將包含與整個模式匹配的文本,$matches[1]將包含第一個捕獲的與括號中的模式單元所匹配的內(nèi)容,以此類推。該函數(shù)只 作一次匹配,最終返回0或1的匹配結(jié)果數(shù)。代碼6.1給出preg_match()函數(shù)的一段代碼示例。
代碼6.1 日期時間的匹配
代碼如下:
<?php
//需要匹配的字符串。date函數(shù)返回當(dāng)前時間
$content = "Current date and time is ".date("Y-m-d h:i a").", we are learning PHP together.";
//使用通常的方法匹配時間
if (preg_match ("http://d{4}-/d{2}-/d{2} /d{2}:/d{2} [ap]m/", $content, $m))
{
echo "匹配的時間是:" .$m[0]. "/n";
}
//由于時間的模式明顯,也可以簡單的匹配
if (preg_match ("/([/d-]{10}) ([/d:]{5} [ap]m)/", $content, $m))
{
echo "當(dāng)前日期是:" .$m[1]. "/n";
echo "當(dāng)前時間是:" .$m[2]. "/n";
}
?>
這是一個簡單動態(tài)文本串匹配實例。假設(shè)當(dāng)前系統(tǒng)時間是“2006年8月17日13點25分”,將輸出如下的內(nèi)容。
匹配的時間是:2006-08-17 01:25 pm
當(dāng)前日期是:2006-08-17
當(dāng)前時間是:01:25 pm
2.ereg()和eregi()
ereg()是POSIX擴展庫中正則表達式的匹配函數(shù)。eregi()是ereg()函數(shù)的忽略大小寫的版 本。二者與preg_match的功能類似,但函數(shù)返回的是一個布爾值,表明匹配成功與否。需要說明的是,POSIX擴展庫函數(shù)的第一個參數(shù)接受的是正則 表達式字符串,即不需要使用分界符。例如,代碼6.2是一個關(guān)于文件名安全檢驗的方法。
代碼6.2 文件名的安全檢驗
代碼如下:
<?php
$username = $_SERVER['REMOTE_USER'];
$filename = $_GET['file'];
//對文件名進行過濾,以保證系統(tǒng)安全
if (!ereg('^[^./][^/]*$', $userfile))
{
die('這不是一個非法的文件名!');
}
//對用戶名進行過濾
if (!ereg('^[^./][^/]*$', $username))
{
die('這不是一個無效的用戶名');
}
//通過安全過濾,拼合文件路徑
$thefile = "/home/$username/$filename";
?>
通常情況下,使用與Perl兼容的正則表達式匹配函數(shù)perg_match(),將比使用ereg()或eregi()的速度更快。如果只是查找一個字符串中是否包含某個子字符串,建議使用strstr()或strpos()函數(shù)。
正則表達式的替換
1.ereg_replace()和eregi_replace()
函數(shù)原型:string ereg_replace (string $pattern, string $replacement, string $string)
string eregi_replace (string $pattern, string $replacement, string $string)
ereg_replace()在$string中搜索模式字符串$pattern,并將所匹配結(jié)果替換 為$replacement。當(dāng)$pattern中包含模式單元(或子模式)時,$replacement中形如“/1”或“$1”的位置將依次被這些子 模式所匹配的內(nèi)容替換。而“/0”或“$0”是指整個的匹配字符串的內(nèi)容。需要注意的是,在雙引號中反斜線作為轉(zhuǎn)義符使用,所以必須使用“//0”,“ //1”的形式。
eregi_replace()和ereg_replace()的功能一致,只是前者忽略大小寫。代碼6.6是本函數(shù)的應(yīng)用實例,這段代碼演示了如何對程序源代碼做簡單的清理工作。
代碼6.6 源代碼的清理
代碼如下:
<?php
$lines = file('source.php'); //將文件讀入數(shù)組中
for($i=0; $i<count($lines); $i++)
{
//將行末以“//”或“#”開頭的注釋去掉
$lines[$i] = eregi_replace("(////|#).*$", "", $lines[$i]);
//將行末的空白消除
$lines[$i] = eregi_replace("[ /n/r/t/v/f]*$", "/r/n", $lines[$i]);
}
//整理后輸出到頁面
echo htmlspecialchars(join("",$lines));
?>
2.preg_replace()
函數(shù)原型:mixed preg_replace (mixed $pattern, mixed $replacement, mixed $subject [, int $limit])
preg_replace較ereg_replace的功能更加強大。其前三個參數(shù)均可以使用數(shù)組;第四個參數(shù)$limit可以設(shè)置替換的次數(shù),默認(rèn)為全部替換。代碼6.7是一個數(shù)組替換的應(yīng)用實例。
代碼6.7 數(shù)組替換
代碼如下:
<?php
//字符串
$string = "Name: {Name}<br>/nEmail: {Email}<br>/nAddress: {Address}<br>/n";
//模式
$patterns =array(
"/{Address}/",
"/{Name}/",
"/{Email}/"
);
//替換字串
$replacements = array (
"No.5, Wilson St., New York, U.S.A",
"Thomas Ching",
"tom@emailaddress.com",
);
//輸出模式替換結(jié)果
print preg_replace($patterns, $replacements, $string);
?>
輸出結(jié)果如下。
Name: Thomas Ching",
Email: tom@emailaddress.com
Address: No.5, Wilson St., New York, U.S.A
在preg_replace的正則表達式中可以使用模式修正符“e”。其作用是將匹配結(jié)果用作表達式,并且可以進行重新運算。例如:
代碼如下:
<?php
$html_body = “<HTML><Body><H1>TEST</H1>My Picture<Img src=”my.gif”></Body></HTML>”;
//輸出結(jié)果中HTML標(biāo)簽將全部為小寫字母
echo preg_replace (
"/(<//?)(/w+)([^>]*>)/e",
"'//1'.strtolower('//2').'//3'", //此處的模式變量//2將被strtolower轉(zhuǎn)換為小寫字符
$html_body);
?>
提示
preg_replace函數(shù)使用了Perl兼容正則表達式語法,通常是比ereg_replace更快的替代方案。如果僅對字符串做簡單的替換,可以使用str_replace函數(shù)。
更多信息請查看IT技術(shù)專欄