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