PHP ereg_replace()函數(shù)的工作原理解析
我們在學(xué)習(xí)PHP語言的時候,經(jīng)常會和各種各樣的函數(shù)打交道,如何正確的理解這些函數(shù),對于我們的整個編碼程序有很大的幫助。今天我們就要為大家具體介紹有關(guān)PHP ereg_replace()函數(shù)的用法。
#t#PHP ereg_replace()函數(shù)原型:string ereg_replace (string $pattern, string $replacement, string $string)
string eregi_replace (string $pattern, string $replacement, string $string)
PHP ereg_replace()函數(shù)c,并將所匹配結(jié)果替換為$replacement。當(dāng)$pattern中包含模式單元(或子模式)時,$replacement中形如“\1”或“$1”的位置將依次被這些子模式所匹配的內(nèi)容替換。而“\0”或“$0”是指整個的匹配字符串的內(nèi)容。需要注意的是,在雙引號中反斜線作為轉(zhuǎn)義符使用,所以必須使用“\\0”,“\\1”的形式。
eregi_replace()和PHP ereg_replace()函數(shù)的功能一致,只是前者忽略大小寫。代碼6.6是本函數(shù)的應(yīng)用實(shí)例,這段代碼演示了如何對程序源代碼做簡單的清理工作。
PHP ereg_replace()函數(shù)代碼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));
- ?>
以上就是PHP ereg_replace()函數(shù)的相關(guān)用法介紹。