xref: /dokuwiki/inc/Utf8/PhpString.php (revision cb57f36620bfb1d791ab3659f81ae457af4c5f6e)
1f41bbe4cSAndreas Gohr<?php
2f41bbe4cSAndreas Gohr
3f41bbe4cSAndreas Gohrnamespace dokuwiki\Utf8;
4f41bbe4cSAndreas Gohr
5f41bbe4cSAndreas Gohr/**
6f41bbe4cSAndreas Gohr * UTF-8 aware equivalents to PHP's string functions
7f41bbe4cSAndreas Gohr */
8f41bbe4cSAndreas Gohrclass PhpString
9f41bbe4cSAndreas Gohr{
10f41bbe4cSAndreas Gohr    /**
11f41bbe4cSAndreas Gohr     * A locale independent basename() implementation
12f41bbe4cSAndreas Gohr     *
13f41bbe4cSAndreas Gohr     * works around a bug in PHP's basename() implementation
14f41bbe4cSAndreas Gohr     *
15f41bbe4cSAndreas Gohr     * @param string $path A path
16f41bbe4cSAndreas Gohr     * @param string $suffix If the name component ends in suffix this will also be cut off
17f41bbe4cSAndreas Gohr     * @return string
18ffdb5936SAndreas Gohr     * @link   https://bugs.php.net/bug.php?id=37738
19ffdb5936SAndreas Gohr     *
20ffdb5936SAndreas Gohr     * @see basename()
21f41bbe4cSAndreas Gohr     */
22f41bbe4cSAndreas Gohr    public static function basename($path, $suffix = '')
23f41bbe4cSAndreas Gohr    {
24f41bbe4cSAndreas Gohr        $path = trim($path, '\\/');
25f41bbe4cSAndreas Gohr        $rpos = max(strrpos($path, '/'), strrpos($path, '\\'));
26f41bbe4cSAndreas Gohr        if ($rpos) {
27f41bbe4cSAndreas Gohr            $path = substr($path, $rpos + 1);
28f41bbe4cSAndreas Gohr        }
29f41bbe4cSAndreas Gohr
30*cb57f366Sfiwswe        $suflen = strlen($suffix);
31*cb57f366Sfiwswe        if ($suflen && str_ends_with($path, $suffix)) {
32*cb57f366Sfiwswe            $path = substr($path, 0, -$suflen);
33f41bbe4cSAndreas Gohr        }
34f41bbe4cSAndreas Gohr
35f41bbe4cSAndreas Gohr        return $path;
36f41bbe4cSAndreas Gohr    }
37f41bbe4cSAndreas Gohr
38f41bbe4cSAndreas Gohr    /**
39f41bbe4cSAndreas Gohr     * Unicode aware replacement for strlen()
40f41bbe4cSAndreas Gohr     *
41f41bbe4cSAndreas Gohr     * utf8_decode() converts characters that are not in ISO-8859-1
42c7acaa16SAndreas Gohr     * to '?', which, for the purpose of counting, is alright
43f41bbe4cSAndreas Gohr     *
44f41bbe4cSAndreas Gohr     * @param string $string
45f41bbe4cSAndreas Gohr     * @return int
46ffdb5936SAndreas Gohr     * @see    utf8_decode()
47ffdb5936SAndreas Gohr     *
48ffdb5936SAndreas Gohr     * @author <chernyshevsky at hotmail dot com>
49ffdb5936SAndreas Gohr     * @see    strlen()
50f41bbe4cSAndreas Gohr     */
51f41bbe4cSAndreas Gohr    public static function strlen($string)
52f41bbe4cSAndreas Gohr    {
53f41bbe4cSAndreas Gohr        if (UTF8_MBSTRING) {
54f41bbe4cSAndreas Gohr            return mb_strlen($string, 'UTF-8');
55f41bbe4cSAndreas Gohr        }
56f41bbe4cSAndreas Gohr
57f41bbe4cSAndreas Gohr        if (function_exists('iconv_strlen')) {
58f41bbe4cSAndreas Gohr            return iconv_strlen($string, 'UTF-8');
59f41bbe4cSAndreas Gohr        }
60f41bbe4cSAndreas Gohr
61c7acaa16SAndreas Gohr        // utf8_decode is deprecated
62c7acaa16SAndreas Gohr        if (function_exists('utf8_decode')) {
63c7acaa16SAndreas Gohr            return strlen(utf8_decode($string));
64c7acaa16SAndreas Gohr        }
65c7acaa16SAndreas Gohr
66f41bbe4cSAndreas Gohr        return strlen($string);
67f41bbe4cSAndreas Gohr    }
68f41bbe4cSAndreas Gohr
69f41bbe4cSAndreas Gohr    /**
70f41bbe4cSAndreas Gohr     * UTF-8 aware alternative to substr
71f41bbe4cSAndreas Gohr     *
72f41bbe4cSAndreas Gohr     * Return part of a string given character offset (and optionally length)
73f41bbe4cSAndreas Gohr     *
74f41bbe4cSAndreas Gohr     * @param string $str
75f41bbe4cSAndreas Gohr     * @param int $offset number of UTF-8 characters offset (from left)
76f41bbe4cSAndreas Gohr     * @param int $length (optional) length in UTF-8 characters from offset
77f41bbe4cSAndreas Gohr     * @return string
78ffdb5936SAndreas Gohr     * @author Harry Fuecks <hfuecks@gmail.com>
79ffdb5936SAndreas Gohr     * @author Chris Smith <chris@jalakai.co.uk>
80ffdb5936SAndreas Gohr     *
81f41bbe4cSAndreas Gohr     */
82f41bbe4cSAndreas Gohr    public static function substr($str, $offset, $length = null)
83f41bbe4cSAndreas Gohr    {
84f41bbe4cSAndreas Gohr        if (UTF8_MBSTRING) {
85f41bbe4cSAndreas Gohr            if ($length === null) {
86f41bbe4cSAndreas Gohr                return mb_substr($str, $offset);
87f41bbe4cSAndreas Gohr            }
88f41bbe4cSAndreas Gohr
89f41bbe4cSAndreas Gohr            return mb_substr($str, $offset, $length);
90f41bbe4cSAndreas Gohr        }
91f41bbe4cSAndreas Gohr
92f41bbe4cSAndreas Gohr        /*
93f41bbe4cSAndreas Gohr         * Notes:
94f41bbe4cSAndreas Gohr         *
95f41bbe4cSAndreas Gohr         * no mb string support, so we'll use pcre regex's with 'u' flag
96f41bbe4cSAndreas Gohr         * pcre only supports repetitions of less than 65536, in order to accept up to MAXINT values for
97f41bbe4cSAndreas Gohr         * offset and length, we'll repeat a group of 65535 characters when needed (ok, up to MAXINT-65536)
98f41bbe4cSAndreas Gohr         *
99f41bbe4cSAndreas Gohr         * substr documentation states false can be returned in some cases (e.g. offset > string length)
100f41bbe4cSAndreas Gohr         * mb_substr never returns false, it will return an empty string instead.
101f41bbe4cSAndreas Gohr         *
102f41bbe4cSAndreas Gohr         * calculating the number of characters in the string is a relatively expensive operation, so
103f41bbe4cSAndreas Gohr         * we only carry it out when necessary. It isn't necessary for +ve offsets and no specified length
104f41bbe4cSAndreas Gohr         */
105f41bbe4cSAndreas Gohr
106f41bbe4cSAndreas Gohr        // cast parameters to appropriate types to avoid multiple notices/warnings
107f41bbe4cSAndreas Gohr        $str = (string)$str;                          // generates E_NOTICE for PHP4 objects, but not PHP5 objects
108f41bbe4cSAndreas Gohr        $offset = (int)$offset;
109f41bbe4cSAndreas Gohr        if ($length !== null) $length = (int)$length;
110f41bbe4cSAndreas Gohr
111f41bbe4cSAndreas Gohr        // handle trivial cases
112f41bbe4cSAndreas Gohr        if ($length === 0) return '';
113f41bbe4cSAndreas Gohr        if ($offset < 0 && $length < 0 && $length < $offset) return '';
114f41bbe4cSAndreas Gohr
115f41bbe4cSAndreas Gohr        $offset_pattern = '';
116f41bbe4cSAndreas Gohr        $length_pattern = '';
117f41bbe4cSAndreas Gohr
118f41bbe4cSAndreas Gohr        // normalise -ve offsets (we could use a tail anchored pattern, but they are horribly slow!)
119f41bbe4cSAndreas Gohr        if ($offset < 0) {
120f41bbe4cSAndreas Gohr            $strlen = self::strlen($str);        // see notes
121f41bbe4cSAndreas Gohr            $offset = $strlen + $offset;
122f41bbe4cSAndreas Gohr            if ($offset < 0) $offset = 0;
123f41bbe4cSAndreas Gohr        }
124f41bbe4cSAndreas Gohr
125f41bbe4cSAndreas Gohr        // establish a pattern for offset, a non-captured group equal in length to offset
126f41bbe4cSAndreas Gohr        if ($offset > 0) {
127f41bbe4cSAndreas Gohr            $Ox = (int)($offset / 65535);
128f41bbe4cSAndreas Gohr            $Oy = $offset % 65535;
129f41bbe4cSAndreas Gohr
130f41bbe4cSAndreas Gohr            if ($Ox) $offset_pattern = '(?:.{65535}){' . $Ox . '}';
131f41bbe4cSAndreas Gohr            $offset_pattern = '^(?:' . $offset_pattern . '.{' . $Oy . '})';
132f41bbe4cSAndreas Gohr        } else {
133f41bbe4cSAndreas Gohr            $offset_pattern = '^';                      // offset == 0; just anchor the pattern
134f41bbe4cSAndreas Gohr        }
135f41bbe4cSAndreas Gohr
136f41bbe4cSAndreas Gohr        // establish a pattern for length
137f41bbe4cSAndreas Gohr        if ($length === null) {
138f41bbe4cSAndreas Gohr            $length_pattern = '(.*)$';                  // the rest of the string
139f41bbe4cSAndreas Gohr        } else {
140f41bbe4cSAndreas Gohr            if (!isset($strlen)) $strlen = self::strlen($str);    // see notes
141f41bbe4cSAndreas Gohr            if ($offset > $strlen) return '';           // another trivial case
142f41bbe4cSAndreas Gohr
143f41bbe4cSAndreas Gohr            if ($length > 0) {
144ffdb5936SAndreas Gohr                // reduce any length that would go past the end of the string
145ffdb5936SAndreas Gohr                $length = min($strlen - $offset, $length);
146f41bbe4cSAndreas Gohr                $Lx = (int)($length / 65535);
147f41bbe4cSAndreas Gohr                $Ly = $length % 65535;
148f41bbe4cSAndreas Gohr                // +ve length requires ... a captured group of length characters
149f41bbe4cSAndreas Gohr                if ($Lx) $length_pattern = '(?:.{65535}){' . $Lx . '}';
150f41bbe4cSAndreas Gohr                $length_pattern = '(' . $length_pattern . '.{' . $Ly . '})';
151f41bbe4cSAndreas Gohr            } elseif ($length < 0) {
152f41bbe4cSAndreas Gohr                if ($length < ($offset - $strlen)) return '';
153f41bbe4cSAndreas Gohr                $Lx = (int)((-$length) / 65535);
154f41bbe4cSAndreas Gohr                $Ly = (-$length) % 65535;
155f41bbe4cSAndreas Gohr                // -ve length requires ... capture everything except a group of -length characters
156f41bbe4cSAndreas Gohr                //                         anchored at the tail-end of the string
157f41bbe4cSAndreas Gohr                if ($Lx) $length_pattern = '(?:.{65535}){' . $Lx . '}';
158f41bbe4cSAndreas Gohr                $length_pattern = '(.*)(?:' . $length_pattern . '.{' . $Ly . '})$';
159f41bbe4cSAndreas Gohr            }
160f41bbe4cSAndreas Gohr        }
161f41bbe4cSAndreas Gohr
162f41bbe4cSAndreas Gohr        if (!preg_match('#' . $offset_pattern . $length_pattern . '#us', $str, $match)) return '';
163f41bbe4cSAndreas Gohr        return $match[1];
164f41bbe4cSAndreas Gohr    }
165f41bbe4cSAndreas Gohr
1668a9a2e3dSAndreas Gohr    // phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
167f41bbe4cSAndreas Gohr    /**
168f41bbe4cSAndreas Gohr     * Unicode aware replacement for substr_replace()
169f41bbe4cSAndreas Gohr     *
170f41bbe4cSAndreas Gohr     * @param string $string input string
171f41bbe4cSAndreas Gohr     * @param string $replacement the replacement
172f41bbe4cSAndreas Gohr     * @param int $start the replacing will begin at the start'th offset into string.
173f41bbe4cSAndreas Gohr     * @param int $length If given and is positive, it represents the length of the portion of string which is
174f41bbe4cSAndreas Gohr     *                            to be replaced. If length is zero then this function will have the effect of inserting
175f41bbe4cSAndreas Gohr     *                            replacement into string at the given start offset.
176f41bbe4cSAndreas Gohr     * @return string
177ffdb5936SAndreas Gohr     * @see    substr_replace()
178ffdb5936SAndreas Gohr     *
179ffdb5936SAndreas Gohr     * @author Andreas Gohr <andi@splitbrain.org>
180f41bbe4cSAndreas Gohr     */
181f41bbe4cSAndreas Gohr    public static function substr_replace($string, $replacement, $start, $length = 0)
182f41bbe4cSAndreas Gohr    {
183f41bbe4cSAndreas Gohr        $ret = '';
184f41bbe4cSAndreas Gohr        if ($start > 0) $ret .= self::substr($string, 0, $start);
185f41bbe4cSAndreas Gohr        $ret .= $replacement;
186f41bbe4cSAndreas Gohr        $ret .= self::substr($string, $start + $length);
187f41bbe4cSAndreas Gohr        return $ret;
188f41bbe4cSAndreas Gohr    }
1898a9a2e3dSAndreas Gohr    // phpcs:enable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
190f41bbe4cSAndreas Gohr
191f41bbe4cSAndreas Gohr    /**
192f41bbe4cSAndreas Gohr     * Unicode aware replacement for ltrim()
193f41bbe4cSAndreas Gohr     *
194f41bbe4cSAndreas Gohr     * @param string $str
195f41bbe4cSAndreas Gohr     * @param string $charlist
196f41bbe4cSAndreas Gohr     * @return string
197ffdb5936SAndreas Gohr     * @see    ltrim()
198ffdb5936SAndreas Gohr     *
199ffdb5936SAndreas Gohr     * @author Andreas Gohr <andi@splitbrain.org>
200f41bbe4cSAndreas Gohr     */
201f41bbe4cSAndreas Gohr    public static function ltrim($str, $charlist = '')
202f41bbe4cSAndreas Gohr    {
203f41bbe4cSAndreas Gohr        if ($charlist === '') return ltrim($str);
204f41bbe4cSAndreas Gohr
205f41bbe4cSAndreas Gohr        //quote charlist for use in a characterclass
206f41bbe4cSAndreas Gohr        $charlist = preg_replace('!([\\\\\\-\\]\\[/])!', '\\\${1}', $charlist);
207f41bbe4cSAndreas Gohr
208f41bbe4cSAndreas Gohr        return preg_replace('/^[' . $charlist . ']+/u', '', $str);
209f41bbe4cSAndreas Gohr    }
210f41bbe4cSAndreas Gohr
211f41bbe4cSAndreas Gohr    /**
212f41bbe4cSAndreas Gohr     * Unicode aware replacement for rtrim()
213f41bbe4cSAndreas Gohr     *
214f41bbe4cSAndreas Gohr     * @param string $str
215f41bbe4cSAndreas Gohr     * @param string $charlist
216f41bbe4cSAndreas Gohr     * @return string
217ffdb5936SAndreas Gohr     * @see    rtrim()
218ffdb5936SAndreas Gohr     *
219ffdb5936SAndreas Gohr     * @author Andreas Gohr <andi@splitbrain.org>
220f41bbe4cSAndreas Gohr     */
221f41bbe4cSAndreas Gohr    public static function rtrim($str, $charlist = '')
222f41bbe4cSAndreas Gohr    {
223f41bbe4cSAndreas Gohr        if ($charlist === '') return rtrim($str);
224f41bbe4cSAndreas Gohr
225f41bbe4cSAndreas Gohr        //quote charlist for use in a characterclass
226f41bbe4cSAndreas Gohr        $charlist = preg_replace('!([\\\\\\-\\]\\[/])!', '\\\${1}', $charlist);
227f41bbe4cSAndreas Gohr
228f41bbe4cSAndreas Gohr        return preg_replace('/[' . $charlist . ']+$/u', '', $str);
229f41bbe4cSAndreas Gohr    }
230f41bbe4cSAndreas Gohr
231f41bbe4cSAndreas Gohr    /**
232f41bbe4cSAndreas Gohr     * Unicode aware replacement for trim()
233f41bbe4cSAndreas Gohr     *
234f41bbe4cSAndreas Gohr     * @param string $str
235f41bbe4cSAndreas Gohr     * @param string $charlist
236f41bbe4cSAndreas Gohr     * @return string
237ffdb5936SAndreas Gohr     * @see    trim()
238ffdb5936SAndreas Gohr     *
239ffdb5936SAndreas Gohr     * @author Andreas Gohr <andi@splitbrain.org>
240f41bbe4cSAndreas Gohr     */
241f41bbe4cSAndreas Gohr    public static function trim($str, $charlist = '')
242f41bbe4cSAndreas Gohr    {
243f41bbe4cSAndreas Gohr        if ($charlist === '') return trim($str);
244f41bbe4cSAndreas Gohr
245f41bbe4cSAndreas Gohr        return self::ltrim(self::rtrim($str, $charlist), $charlist);
246f41bbe4cSAndreas Gohr    }
247f41bbe4cSAndreas Gohr
248f41bbe4cSAndreas Gohr    /**
249f41bbe4cSAndreas Gohr     * This is a unicode aware replacement for strtolower()
250f41bbe4cSAndreas Gohr     *
251f41bbe4cSAndreas Gohr     * Uses mb_string extension if available
252f41bbe4cSAndreas Gohr     *
253f41bbe4cSAndreas Gohr     * @param string $string
254f41bbe4cSAndreas Gohr     * @return string
2558cbc5ee8SAndreas Gohr     * @see    \dokuwiki\Utf8\PhpString::strtoupper()
256ffdb5936SAndreas Gohr     *
257ffdb5936SAndreas Gohr     * @author Leo Feyer <leo@typolight.org>
258ffdb5936SAndreas Gohr     * @see    strtolower()
259f41bbe4cSAndreas Gohr     */
260f41bbe4cSAndreas Gohr    public static function strtolower($string)
261f41bbe4cSAndreas Gohr    {
262bf8f8509SAndreas Gohr        if ($string === null) return ''; // pre-8.1 behaviour
263f41bbe4cSAndreas Gohr        if (UTF8_MBSTRING) {
264f41bbe4cSAndreas Gohr            if (class_exists('Normalizer', $autoload = false)) {
265f41bbe4cSAndreas Gohr                return \Normalizer::normalize(mb_strtolower($string, 'utf-8'));
266f41bbe4cSAndreas Gohr            }
267f41bbe4cSAndreas Gohr            return (mb_strtolower($string, 'utf-8'));
268f41bbe4cSAndreas Gohr        }
269f41bbe4cSAndreas Gohr        return strtr($string, Table::upperCaseToLowerCase());
270f41bbe4cSAndreas Gohr    }
271f41bbe4cSAndreas Gohr
272f41bbe4cSAndreas Gohr    /**
273f41bbe4cSAndreas Gohr     * This is a unicode aware replacement for strtoupper()
274f41bbe4cSAndreas Gohr     *
275f41bbe4cSAndreas Gohr     * Uses mb_string extension if available
276f41bbe4cSAndreas Gohr     *
277f41bbe4cSAndreas Gohr     * @param string $string
278f41bbe4cSAndreas Gohr     * @return string
2798cbc5ee8SAndreas Gohr     * @see    \dokuwiki\Utf8\PhpString::strtoupper()
280ffdb5936SAndreas Gohr     *
281ffdb5936SAndreas Gohr     * @author Leo Feyer <leo@typolight.org>
282ffdb5936SAndreas Gohr     * @see    strtoupper()
283f41bbe4cSAndreas Gohr     */
284f41bbe4cSAndreas Gohr    public static function strtoupper($string)
285f41bbe4cSAndreas Gohr    {
286f41bbe4cSAndreas Gohr        if (UTF8_MBSTRING) return mb_strtoupper($string, 'utf-8');
287f41bbe4cSAndreas Gohr
288f41bbe4cSAndreas Gohr        return strtr($string, Table::lowerCaseToUpperCase());
289f41bbe4cSAndreas Gohr    }
290f41bbe4cSAndreas Gohr
291f41bbe4cSAndreas Gohr
292f41bbe4cSAndreas Gohr    /**
293f41bbe4cSAndreas Gohr     * UTF-8 aware alternative to ucfirst
294f41bbe4cSAndreas Gohr     * Make a string's first character uppercase
295f41bbe4cSAndreas Gohr     *
296f41bbe4cSAndreas Gohr     * @param string $str
297f41bbe4cSAndreas Gohr     * @return string with first character as upper case (if applicable)
298ffdb5936SAndreas Gohr     * @author Harry Fuecks
299ffdb5936SAndreas Gohr     *
300f41bbe4cSAndreas Gohr     */
301f41bbe4cSAndreas Gohr    public static function ucfirst($str)
302f41bbe4cSAndreas Gohr    {
303f41bbe4cSAndreas Gohr        switch (self::strlen($str)) {
304f41bbe4cSAndreas Gohr            case 0:
305f41bbe4cSAndreas Gohr                return '';
306f41bbe4cSAndreas Gohr            case 1:
307f41bbe4cSAndreas Gohr                return self::strtoupper($str);
308f41bbe4cSAndreas Gohr            default:
309f41bbe4cSAndreas Gohr                preg_match('/^(.{1})(.*)$/us', $str, $matches);
310f41bbe4cSAndreas Gohr                return self::strtoupper($matches[1]) . $matches[2];
311f41bbe4cSAndreas Gohr        }
312f41bbe4cSAndreas Gohr    }
313f41bbe4cSAndreas Gohr
314f41bbe4cSAndreas Gohr    /**
315f41bbe4cSAndreas Gohr     * UTF-8 aware alternative to ucwords
316f41bbe4cSAndreas Gohr     * Uppercase the first character of each word in a string
317f41bbe4cSAndreas Gohr     *
318ffdb5936SAndreas Gohr     * @param string $str
319ffdb5936SAndreas Gohr     * @return string with first char of each word uppercase
320f41bbe4cSAndreas Gohr     * @author Harry Fuecks
321f41bbe4cSAndreas Gohr     * @see http://php.net/ucwords
322f41bbe4cSAndreas Gohr     *
323f41bbe4cSAndreas Gohr     */
324f41bbe4cSAndreas Gohr    public static function ucwords($str)
325f41bbe4cSAndreas Gohr    {
326f41bbe4cSAndreas Gohr        // Note: [\x0c\x09\x0b\x0a\x0d\x20] matches;
327f41bbe4cSAndreas Gohr        // form feeds, horizontal tabs, vertical tabs, linefeeds and carriage returns
328f41bbe4cSAndreas Gohr        // This corresponds to the definition of a "word" defined at http://php.net/ucwords
329f41bbe4cSAndreas Gohr        $pattern = '/(^|([\x0c\x09\x0b\x0a\x0d\x20]+))([^\x0c\x09\x0b\x0a\x0d\x20]{1})[^\x0c\x09\x0b\x0a\x0d\x20]*/u';
330f41bbe4cSAndreas Gohr
331f41bbe4cSAndreas Gohr        return preg_replace_callback(
332f41bbe4cSAndreas Gohr            $pattern,
333f41bbe4cSAndreas Gohr            function ($matches) {
334f41bbe4cSAndreas Gohr                $leadingws = $matches[2];
335f41bbe4cSAndreas Gohr                $ucfirst = self::strtoupper($matches[3]);
336f41bbe4cSAndreas Gohr                $ucword = self::substr_replace(ltrim($matches[0]), $ucfirst, 0, 1);
337f41bbe4cSAndreas Gohr                return $leadingws . $ucword;
338f41bbe4cSAndreas Gohr            },
339f41bbe4cSAndreas Gohr            $str
340f41bbe4cSAndreas Gohr        );
341f41bbe4cSAndreas Gohr    }
342f41bbe4cSAndreas Gohr
343f41bbe4cSAndreas Gohr    /**
344f41bbe4cSAndreas Gohr     * This is an Unicode aware replacement for strpos
345f41bbe4cSAndreas Gohr     *
346f41bbe4cSAndreas Gohr     * @param string $haystack
347f41bbe4cSAndreas Gohr     * @param string $needle
348f41bbe4cSAndreas Gohr     * @param integer $offset
349f41bbe4cSAndreas Gohr     * @return integer
350ffdb5936SAndreas Gohr     * @author Leo Feyer <leo@typolight.org>
351ffdb5936SAndreas Gohr     * @see    strpos()
352ffdb5936SAndreas Gohr     *
353f41bbe4cSAndreas Gohr     */
354f41bbe4cSAndreas Gohr    public static function strpos($haystack, $needle, $offset = 0)
355f41bbe4cSAndreas Gohr    {
356f41bbe4cSAndreas Gohr        $comp = 0;
357f41bbe4cSAndreas Gohr        $length = null;
358f41bbe4cSAndreas Gohr
359f41bbe4cSAndreas Gohr        while ($length === null || $length < $offset) {
360f41bbe4cSAndreas Gohr            $pos = strpos($haystack, $needle, $offset + $comp);
361f41bbe4cSAndreas Gohr
362f41bbe4cSAndreas Gohr            if ($pos === false)
363f41bbe4cSAndreas Gohr                return false;
364f41bbe4cSAndreas Gohr
365f41bbe4cSAndreas Gohr            $length = self::strlen(substr($haystack, 0, $pos));
366f41bbe4cSAndreas Gohr
367f41bbe4cSAndreas Gohr            if ($length < $offset)
368f41bbe4cSAndreas Gohr                $comp = $pos - $length;
369f41bbe4cSAndreas Gohr        }
370f41bbe4cSAndreas Gohr
371f41bbe4cSAndreas Gohr        return $length;
372f41bbe4cSAndreas Gohr    }
373f41bbe4cSAndreas Gohr}
374