diff options
author | Björn Brala <github@bjornbrala.com> | 2020-04-09 11:20:00 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-09 11:20:00 +0200 |
commit | 143f5056efc5e5efedb7c2faf54b53a8e79a7210 (patch) | |
tree | 9481d0ff2e039d27e1e768e35ee76e29b5588741 | |
parent | 0a23e1c31a43d0da0cf3619e71f73571d702fdab (diff) | |
parent | 2f04d3ebd05f581823ccfcad4b6e9d0d22f7985f (diff) | |
download | php7-ereg-shim-master.zip php7-ereg-shim-master.tar.gz php7-ereg-shim-master.tar.bz2 |
Merge pull request #2 from rmpel/masterHEAD1.1.0origin/masterorigin/HEADmaster
use a boundary that does not exist in the pattern.
-rw-r--r-- | lib/ereg.php | 42 |
1 files changed, 36 insertions, 6 deletions
diff --git a/lib/ereg.php b/lib/ereg.php index 72a8943..e14b9b9 100644 --- a/lib/ereg.php +++ b/lib/ereg.php @@ -3,32 +3,62 @@ namespace { if (!function_exists('ereg')) { function ereg($pattern, $subject, &$matches = array()) { - return preg_match('/'.$pattern.'/', $subject, $matches); + $boundary = _ereg_determine_boundary($pattern); + return preg_match($boundary . $pattern . $boundary, $subject, $matches); } function eregi($pattern, $subject, &$matches = array()) { - return preg_match('/'.$pattern.'/i', $subject, $matches); + $boundary = _ereg_determine_boundary($pattern); + return preg_match($boundary . $pattern . $boundary . 'i', $subject, $matches); } function ereg_replace($pattern, $replacement, $string) { - return preg_replace('/'.$pattern.'/', $replacement, $string); + $boundary = _ereg_determine_boundary($pattern); + return preg_replace($boundary . $pattern . $boundary, $replacement, $string); } function eregi_replace($pattern, $replacement, $string) { - return preg_replace('/'.$pattern.'/i', $replacement, $string); + $boundary = _ereg_determine_boundary($pattern); + return preg_replace($boundary . $pattern . $boundary . 'i', $replacement, $string); } function split($pattern, $subject, $limit = -1) { - return preg_split('/'.$pattern.'/', $subject, $limit); + $boundary = _ereg_determine_boundary($pattern); + return preg_split($boundary . $pattern . $boundary, $subject, $limit); } function spliti($pattern, $subject, $limit = -1) { - return preg_split('/'.$pattern.'/i', $subject, $limit); + $boundary = _ereg_determine_boundary($pattern); + return preg_split($boundary . $pattern . $boundary . 'i', $subject, $limit); + } + + /** + * @method _ereg_determine_boundary Determine a valid boundary based on the pattern. + * + * @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 to see where the incompatible ereg-style function call is made."); } } } |