diff options
-rw-r--r-- | lib/ereg.php | 58 |
1 files changed, 35 insertions, 23 deletions
diff --git a/lib/ereg.php b/lib/ereg.php index 4feb5e4..1f1ee07 100644 --- a/lib/ereg.php +++ b/lib/ereg.php @@ -3,50 +3,62 @@ namespace { if (!function_exists('ereg')) { function ereg($pattern, $subject, &$matches = array()) { - foreach (array('/', '@', '#', '%', '±') as $boundary) - if (false === strpos($pattern, $boundary)) { - return preg_match($boundary.$pattern.$boundary, $subject, $matches); - } + $boundary = _ereg_determine_boundary($pattern); + return preg_match($boundary . $pattern . $boundary, $subject, $matches); } function eregi($pattern, $subject, &$matches = array()) { - foreach (array('/', '@', '#', '%', '±') as $boundary) - if (false === strpos($pattern, $boundary)) { - return preg_match($boundary.$pattern.$boundary.'i', $subject, $matches); - } + $boundary = _ereg_determine_boundary($pattern); + return preg_match($boundary . $pattern . $boundary . 'i', $subject, $matches); } function ereg_replace($pattern, $replacement, $string) { - foreach (array('/', '@', '#', '%', '±') as $boundary) - if (false === strpos($pattern, $boundary)) { - return preg_replace($boundary.$pattern.$boundary, $replacement, $string); - } + $boundary = _ereg_determine_boundary($pattern); + return preg_replace($boundary . $pattern . $boundary, $replacement, $string); } function eregi_replace($pattern, $replacement, $string) { - foreach (array('/', '@', '#', '%', '±') as $boundary) - if (false === strpos($pattern, $boundary)) { - return preg_replace($boundary.$pattern.$boundary.'i', $replacement, $string); - } + $boundary = _ereg_determine_boundary($pattern); + return preg_replace($boundary . $pattern . $boundary . 'i', $replacement, $string); } function split($pattern, $subject, $limit = -1) { - foreach (array('/', '@', '#', '%', '±') as $boundary) - if (false === strpos($pattern, $boundary)) { - return preg_split($boundary.$pattern.$boundary, $subject, $limit); - } + $boundary = _ereg_determine_boundary($pattern); + return preg_split($boundary . $pattern . $boundary, $subject, $limit); } function spliti($pattern, $subject, $limit = -1) { - foreach (array('/', '@', '#', '%', '±') as $boundary) - if (false === strpos($pattern, $boundary)) { - return preg_split($boundary.$pattern.$boundary.'i', $subject, $limit); + $boundary = _ereg_determine_boundary($pattern); + return preg_split($boundary . $pattern . $boundary . 'i', $subject, $limit); + } + + /** + * @method _ereg_determine_boundary Prepare the pattern and determine a valid boundary. + * + * @var string $pattern The possibly escaped pattern to match for. + * @return string a suitable RegEx boundary + * + * One would suggest just using preg_quote, but perhaps the pattern already is quoted, and therefore + * using preg_quote would just double-quote and with that ruin the pattern. + * The trick here is to find a boundary that is not within the pattern. + * I have contemplated using "any ASCII charachter" but that would require detecting character classes + * and that's just too complicated. + * The chance that all of these boundaries will conflict, and if so; new ones can be added :) + */ + function _ereg_determine_boundary($pattern) + { + foreach (array('/', '@', '#', '%', '±') as $boundary) { + if (false === strpos($pattern, $boundary)) { + return $boundary; + } } + + throw new Exception("Very sorry, could not shim the regular expression. Please follow the debug trace two steps back to see where the incompatible ereg-style function call is made."); } } } |