diff options
author | Remon Pel <remon@clearsite.nl> | 2020-03-30 16:51:45 +0200 |
---|---|---|
committer | Remon Pel <remon@clearsite.nl> | 2020-03-30 16:51:45 +0200 |
commit | ae29d8d2e8f4d8cdb321925fb320229140597c09 (patch) | |
tree | db800d08eb2e8e7f9c0737b17550555e60b29550 | |
parent | 368ad2751b651d1ae9c9076d3a30fb69c7c051ae (diff) | |
download | php7-ereg-shim-ae29d8d2e8f4d8cdb321925fb320229140597c09.zip php7-ereg-shim-ae29d8d2e8f4d8cdb321925fb320229140597c09.tar.gz php7-ereg-shim-ae29d8d2e8f4d8cdb321925fb320229140597c09.tar.bz2 |
more readable code
-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."); } } } |