目次
What Is preg_last_error()?
Common Error Codes and Their Meanings
1. PREG_INTERNAL_ERROR (1)
2. PREG_BAD_UTF8_ERROR (5)
3. PREG_BAD_UTF8_OFFSET_ERROR (6)
4. PREG_JIT_STACKLIMIT_ERROR (7)
5. PREG_RECURSION_LIMIT_ERROR (2)
How to Use preg_last_error() Effectively
Pro Tips for Debugging Regex in PHP
Final Notes
ホームページ バックエンド開発 PHPチュートリアル パターンのデバッグ: `preg_last_error()`とその意味のガイド

パターンのデバッグ: `preg_last_error()`とその意味のガイド

Aug 07, 2025 pm 09:35 PM
PHP Regular Expressions

preg_last_error() 返回最后一个 PCRE 函数调用的错误代码,用于精准排查正则表达式错误。1. PREG_INTERNAL_ERROR (1) 表示 PCRE 内部错误,通常需更新 PHP 或报告漏洞;2. PREG_BAD_UTF8_ERROR (5) 表示字符串或模式包含无效 UTF-8,应确保使用正确的编码;3. PREG_BAD_UTF8_OFFSET_ERROR (6) 表示 UTF-8 偏移不合法,应使用 mb_strlen() 等函数校正偏移位置;4. PREG_JIT_STACKLIMIT_ERROR (7) 表示 JIT 栈溢出,可通过禁用 JIT 或简化正则避免;5. PREG_RECURSION_LIMIT_ERROR (2) 表示递归超限,应优化模式或调整 pcre.recursion_limit。使用 preg_last_error() 可区分匹配失败与语法错误,结合 switch 语句处理各类错误,并避免使用 @ 抑制错误,确保问题可追踪,从而提升 PHP 正则处理的稳定性与可维护性。

Debugging Your Patterns: A Guide to `preg_last_error()` and its Meanings

When working with regular expressions in PHP, even a small typo or incorrect syntax can cause a pattern to fail. While PHP’s preg_* functions are powerful, they don’t always make it obvious why a regex failed. That’s where preg_last_error() comes in — it’s a crucial tool for debugging pattern errors.

Debugging Your Patterns: A Guide to `preg_last_error()` and its Meanings

This function returns an integer representing the error code from the last PCRE (Perl Compatible Regular Expressions) function call. By interpreting this code, you can pinpoint what went wrong and fix your regex more efficiently.


What Is preg_last_error()?

After using any preg_* function (like preg_match(), preg_replace(), or preg_split()), if the function returns false, it often means an error occurred. However, false could mean either “no match” or “actual error.” To distinguish between them, call preg_last_error():

Debugging Your Patterns: A Guide to `preg_last_error()` and its Meanings
if (preg_match('/(unbalanced/', $subject) === false) {
    $errorCode = preg_last_error();
    echo "Regex error: " . $errorCode;
}

But numbers aren’t helpful on their own — you need to know what they mean.


Common Error Codes and Their Meanings

Here are the most frequent error codes returned by preg_last_error(), along with explanations and how to fix them:

Debugging Your Patterns: A Guide to `preg_last_error()` and its Meanings

1. PREG_INTERNAL_ERROR (1)

This indicates a PCRE library internal error — very rare in practice.

  • Cause: Usually due to a bug in PHP or PCRE itself.
  • Fix: Update PHP or report the issue. In most cases, this isn’t caused by your code.

2. PREG_BAD_UTF8_ERROR (5)

Your subject string or pattern contains invalid UTF-8 data when using the u modifier.

// Example: Invalid byte sequence
$subject = "café"; // But encoded incorrectly
preg_match('/café/u', $subject);
  • Fix: Ensure your strings are properly UTF-8 encoded.
  • If processing user input, consider using mb_convert_encoding() or utf8_encode() as needed.

Tip: This error can also occur in the pattern itself if you're using non-UTF-8 characters with the u flag.

3. PREG_BAD_UTF8_OFFSET_ERROR (6)

Occurs when using PREG_OFFSET_CAPTURE with u, and the offset doesn't align with UTF-8 character boundaries.

preg_match('/\w+/u', 'café', $matches, PREG_OFFSET_CAPTURE, 3);
// Offset 3 might fall in the middle of 'é' (multi-byte)
  • Fix: Use valid start positions. Better yet, work with code points or validate offsets using mb_strlen().

4. PREG_JIT_STACKLIMIT_ERROR (7)

PCRE’s Just-In-Time (JIT) compiler ran out of stack space.

// Common with very complex or deeply nested patterns
preg_match('/^(a+)+$/', $longString);
  • Cause: Overly complex regex or long input triggering JIT limits.
  • Fix:
    • Disable JIT: Use preg_match($pattern, $subject, $matches, PREG_NO_ERROR | PREG_JIT_STACKLIMIT_ERROR);
    • Or simplify your pattern to avoid catastrophic backtracking.

Note: You can also increase pcre.jit_stacklimit in php.ini, but fixing the pattern is safer.

5. PREG_RECURSION_LIMIT_ERROR (2)

Exceeded the recursion limit during matching (often due to complex subpatterns or backreferences).

// Deeply recursive pattern
preg_match('/(a|b)+(c+)?/', str_repeat('a', 100000));
  • Fix:
    • Simplify the regex.
    • Increase pcre.recursion_limit in php.ini (not recommended for production).
    • Avoid greedy quantifiers on large inputs.

How to Use preg_last_error() Effectively

Always check the result of preg_* functions and use preg_last_error() when results are unexpected.

$pattern = '/[a-z/u'; // Missing closing bracket
$subject = 'hello';

$result = preg_match($pattern, $subject);

if ($result === false) {
    switch (preg_last_error()) {
        case PREG_INTERNAL_ERROR:
            echo "Internal PCRE error";
            break;
        case PREG_BAD_UTF8_ERROR:
            echo "Invalid UTF-8 in subject or pattern";
            break;
        case PREG_BAD_UTF8_OFFSET_ERROR:
            echo "Invalid UTF-8 offset";
            break;
        case PREG_JIT_STACKLIMIT_ERROR:
            echo "JIT stack limit exceeded – consider disabling JIT or simplifying regex";
            break;
        case PREG_RECURSION_LIMIT_ERROR:
            echo "Recursion limit exceeded – check for overly complex patterns";
            break;
        default:
            echo "Unknown error";
    }
} elseif ($result === 0) {
    echo "No match found (but no error)";
}

Pro Tips for Debugging Regex in PHP

  • Test patterns in isolation: Use tools like regex101.com (with PCRE mode) to validate syntax.
  • Avoid @ suppression: Using @preg_match() hides errors — you won’t see warnings or be able to use preg_last_error().
  • Use PREG_NO_ERROR after checks: Once you’ve handled an error, reset expectations.
  • Log errors in production: When a regex fails, log both the error code and the pattern/subject (careful with sensitive data).

Final Notes

preg_last_error() won’t tell you exactly which character broke your pattern, but it narrows down the problem category significantly. Combined with careful pattern design and input validation, it becomes an essential part of robust PHP development.

Use it every time a preg_* function returns false, and you’ll spend less time guessing and more time fixing.

Basically, don’t ignore the error — decode it.

以上がパターンのデバッグ: `preg_last_error()`とその意味のガイドの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。

ホットAIツール

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Clothoff.io

Clothoff.io

AI衣類リムーバー

Video Face Swap

Video Face Swap

完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

ホットツール

メモ帳++7.3.1

メモ帳++7.3.1

使いやすく無料のコードエディター

SublimeText3 中国語版

SublimeText3 中国語版

中国語版、とても使いやすい

ゼンドスタジオ 13.0.1

ゼンドスタジオ 13.0.1

強力な PHP 統合開発環境

ドリームウィーバー CS6

ドリームウィーバー CS6

ビジュアル Web 開発ツール

SublimeText3 Mac版

SublimeText3 Mac版

神レベルのコード編集ソフト(SublimeText3)

`u`モディファイアを解き放つ:PHPでユニコード認識の正規表現に深く潜る `u`モディファイアを解き放つ:PHPでユニコード認識の正規表現に深く潜る Aug 03, 2025 am 06:39 AM

TheumodifierinPHPregexisessentialforproperUTF-8andUnicodesupport.1.ItensuresthepatternandinputstringaretreatedasUTF-8,preventingmisinterpretationofmulti-bytecharacters.2.Withoutu,characterslikeéoremojismaycausemismatchesorfailuresbecausetheengineread

複雑な文字列アサーションの見た目と見た目を習得します 複雑な文字列アサーションの見た目と見た目を習得します Aug 04, 2025 am 06:35 AM

ポジティブアサーション(?= ...)、ネガティブアサーション(?!...)、ポジティブアサーション(??

数値キャプチャを超えて:「preg_match」と `preg_replace`で名前付きグループを活用する 数値キャプチャを超えて:「preg_match」と `preg_replace`で名前付きグループを活用する Aug 04, 2025 pm 03:44 PM

namedcapturegroupsinphpprovideaclearandmaintainablewwaytoextractmatchedtextbyasingingmandinginsteadinsteadedofrelyingonnumericindices.1.use( 'name'pattern)syntaxtodefineNenamedgroupsinpcre.2.inpreg_match

複雑な交換に `preg_replace` vs.` preg_replace_callback_array`を使用するとき 複雑な交換に `preg_replace` vs.` preg_replace_callback_array`を使用するとき Aug 08, 2025 pm 06:10 PM

usepreg_replaceforsimpeltimpatternswapswithtaticReplacementsorbackReferences.2.usepreg_replace_callback_arrayformultiplepatternsRequiringcustomlogicviacallbacks、特にwhen replacementsdependentent、infunctions、infuneditionalling

獣を飼いならす:PCREでの壊滅的なバックトラッキングを緩和する 獣を飼いならす:PCREでの壊滅的なバックトラッキングを緩和する Aug 03, 2025 am 07:17 AM

catastrophicbacktrackingCurswhennestedgreedyquantifierscauseexponentialbacktrackingonfailedmatches、asin^(a)$ ang "aaaax" .2。

高度なパターンコントロール:「x`、 `s」の探索 高度なパターンコントロール:「x`、 `s」の探索 Aug 04, 2025 am 10:54 AM

thex、s、andjmodifiersinperlenhanceregexfficibility:1)thexmodifierallowswhiteSpaceandcomments forreadablepatterns、naverinscapedspacesandabling#コメント、2)themodifiermakentmatchnewlines、

ネストされた構造を解析するためのPCREの再帰パターン ネストされた構造を解析するためのPCREの再帰パターン Aug 11, 2025 am 11:06 AM

pcre'sRecursivePatternSeNableMatchingNestedStructuresSolSheSORBracketsusing(?r)ornaMedReferenceslikelike(?&name)、recursively -applyingthepattern; forexample、^$$)|(?1)$$

PHPの `preg_match_all`を使用して堅牢なログファイルパーサーを作成します PHPの `preg_match_all`を使用して堅牢なログファイルパーサーを作成します Aug 03, 2025 am 09:20 AM

preg_match_all関数を使用して、正規表現と協力して、PHPログファイルを効率的に解析します。 1.最初に、ApacheのCLFなどのログ形式を分析します。 2。名前付きキャプチャグループを使用して通常のパターンを構築して、IP、メソッド、パス、その他のフィールドを抽出します。 3. preg_match_allを使用してpreg_set_orderフラグと協力して、バッチでマルチラインログを解析します。 4.フィールドの欠落や交差列ログなどのエッジケースを処理します。 5.抽出されたデータを変換して入力し、最後に非構造化されたログを構造化された配列データに変換して、さらに処理します。

See all articles