summaryrefslogtreecommitdiffstats
path: root/lib/ereg.php
blob: e14b9b90f4d91c64352b84721041f6d3ee4325a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
namespace {
    if (!function_exists('ereg')) {
        function ereg($pattern, $subject, &$matches = array())
        {
            $boundary = _ereg_determine_boundary($pattern);
            return preg_match($boundary . $pattern . $boundary, $subject, $matches);
        }

        function eregi($pattern, $subject, &$matches = array())
        {
            $boundary = _ereg_determine_boundary($pattern);
            return preg_match($boundary . $pattern . $boundary . 'i', $subject, $matches);
        }

        function ereg_replace($pattern, $replacement, $string)
        {
            $boundary = _ereg_determine_boundary($pattern);
            return preg_replace($boundary . $pattern . $boundary, $replacement, $string);
        }

        function eregi_replace($pattern, $replacement, $string)
        {
            $boundary = _ereg_determine_boundary($pattern);
            return preg_replace($boundary . $pattern . $boundary . 'i', $replacement, $string);
        }

        function split($pattern, $subject, $limit = -1)
        {
            $boundary = _ereg_determine_boundary($pattern);
            return preg_split($boundary . $pattern . $boundary, $subject, $limit);
        }

        function spliti($pattern, $subject, $limit = -1)
        {
            $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.");
        }
    }
}