Regex recompiled in loop (ARCH7073)
هذا المحتوى غير متوفر بلغتك بعد.
Key php.performance.regex-recompiled-in-loop · Severity info · Category performance
A constant pattern is recompiled by preg_match on every iteration. Define the pattern once outside the loop; PHP reuses it.
Incorrect
Section titled “Incorrect”<?phpforeach ($lines as $line) { if (preg_match('/^\d{4}-\d{2}-\d{2}/', $line)) { $dates[] = $line; }}Correct
Section titled “Correct”<?php$pattern = '/^\d{4}-\d{2}-\d{2}/';foreach ($lines as $line) { if (preg_match($pattern, $line)) { $dates[] = $line; }}Hoist constant regex patterns into a variable outside the loop and reuse it inside.