首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 綜合知識

POSIX 正則 vs PCRE 正則:ereg/ereg_replace 與 preg_match/preg_replace 完整對比

     編輯:PHP 綜合知識

兩套正則家族對照

特性POSIX (ereg 系)PCRE (preg 系)
函數前綴ereg, ereg_replace, split...preg_match, preg_replace, preg_split...
語法標準POSIX 1003.2Perl 兼容 (PCRE)
定界符不需要需要 (如 /pattern/)
效能較慢較快 (C 庫優化)
功能豐富度基礎強大 (斷言、回溯控制、遞歸...)
PHP 狀態PHP 5.3 棄用,7.0 移除標準推薦

POSIX 風格核心函數

ereg - 模式匹配

int ereg(string $pattern, string $string, array &$regs = null)

範例:日期格式轉換 YYYY-MM-DD → DD.MM.YYYY

<?php
if (ereg("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})", $date, $regs)) {
    echo "$regs[3].$regs[2].$regs[1]";
} else {
    echo "Invalid date format: $date";
}
?>

ereg_replace - 模式替換

string ereg_replace(string $pattern, string $replacement, string $string)

範例:

<?php
$string = "This is a test";
echo str_replace(" is", " was", $string);           // 字串替換
echo ereg_replace("( )is", "\\1was", $string);      // 保留空格
echo ereg_replace("(( )is)", "\\2was", $string);    // 使用第二捕獲組

// 整數替換陷阱
$num = 4;
ereg_replace('four', $num, $string);   // 錯誤:把 4 當 ASCII 字符
ereg_replace('four', '4', $string);    // 正確:字串 '4'

// URL 轉超連結
$text = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]", "\\0", $text);
?>

PCRE 風格核心函數 (現代標準)

preg_match - 執行匹配

int preg_match(string $pattern, string $subject, array &$matches = null, int $flags = 0, int $offset = 0)

範例:

<?php
// 不區分大小寫搜尋 "php"
if (preg_match("/php/i", "PHP is the web scripting language")) {
    print "A match was found.";
}

// 單詞邊界 \b:只匹配獨立 "web"
preg_match("/\bweb\b/i", "PHP is the web scripting language"); // 匹配
preg_match("/\bweb\b/i", "PHP is the website scripting language"); // 不匹配

// 從 URL 提取域名
preg_match("/^(http:\/\/)?([^\/]+)/i", "https://www.php.net/index.html", $matches);
$host = $matches[2];
preg_match("/[^\.\/]+\.[^\.\/]+$/", $host, $matches);
echo "domain: {$matches[0]}"; // php.net
?>

preg_replace - 搜尋替換

mixed preg_replace(mixed $pattern, mixed $replacement, mixed $subject, int $limit = -1, int &$count = null)

進階範例:

<?php
// 逆向引用後接數字:${1}1 表示第1組 + 字面數字1
$string = "April 15, 2003";
$pattern = "/(\w+) (\d+), (\d+)/i";
$replacement = "${1}1,\$3";
echo preg_replace($pattern, $replacement, $string); // April1,2003

// 陣列批量替換 (需 ksort 保證順序)
$string = "The quick brown fox";
$patterns = ["/quick/", "/brown/", "/fox/"];
$replacements = ["slow", "black", "bear"];
ksort($patterns); ksort($replacements);
echo preg_replace($patterns, $replacements, $string); // The slow black bear

// /e 修飾符:替換字串作為 PHP 代碼執行 (PHP 7.0+ 移除,改用 callback)
preg_replace("/(<\/?)(\w+)([^>]*>)/e", "'\\1'.strtoupper('\\2').'\\3'", $html_body); // 標籤大寫

// HTML 轉純文本 (完整版)
$search = [
    "']*?>.*?'si",
    "'<[/!]*?[^<>]*?>'si",
    "'([\r\n])[\s]+'",
    "'&(quot|#34);'i", "'&(amp|#38);'i", "'&(lt|#60);'i",
    "'&(gt|#62);'i", "'&(nbsp|#160);'i", "'&#(\d+);'"
];
$replace = ["", "", "\\1", "\"", "&", "<", ">", " ", "chr(\\1)"];
$text = preg_replace($search, $replace, $document);
?>

遷移建議

  1. 全專案替換 ereg → preg_match,ereg_replace → preg_replace
  2. 模式加上定界符:pattern → /pattern/
  3. 字符類別轉換:[[:alpha:]] → [a-zA-Z] 或 \p{L}
  4. 捕獲組引用:\\1 → $1 (推薦) 或 \\1
  5. 移除 /e 修飾符,改用 preg_replace_callback
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved