diff --git a/lib/tests/textlib_test.php b/lib/tests/textlib_test.php index 64f04827188..b02d5b3671e 100644 --- a/lib/tests/textlib_test.php +++ b/lib/tests/textlib_test.php @@ -379,6 +379,17 @@ class core_textlib_testcase extends advanced_testcase { $this->assertSame($textlib->specialtoascii('ábc'), 'abc'); $this->assertSame($textlib->strtotitle('abc ABC'), 'Abc Abc'); } + + /** + * Test strrchr. + */ + public function test_strrchr() { + $str = "Žluťoučký koníček"; + $this->assertSame('koníček', textlib::strrchr($str, 'koní')); + $this->assertSame('Žluťoučký ', textlib::strrchr($str, 'koní', true)); + $this->assertFalse(textlib::strrchr($str, 'A')); + $this->assertFalse(textlib::strrchr($str, 'ç', true)); + } } diff --git a/lib/textlib.class.php b/lib/textlib.class.php index c91c7e5c1f6..190ff78b1bc 100644 --- a/lib/textlib.class.php +++ b/lib/textlib.class.php @@ -238,6 +238,36 @@ class textlib { return $result; } + /** + * Finds the last occurrence of a character in a string within another. + * UTF-8 ONLY safe mb_strrchr(). + * + * @param string $haystack The string from which to get the last occurrence of needle. + * @param string $needle The string to find in haystack. + * @param boolean $part If true, returns the portion before needle, else return the portion after (including needle). + * @return string|false False when not found. + * @since 2.4.6, 2.5.2, 2.6 + */ + public static function strrchr($haystack, $needle, $part = false) { + + if (function_exists('mb_strrchr')) { + return mb_strrchr($haystack, $needle, $part, 'UTF-8'); + } + + $pos = self::strrpos($haystack, $needle); + if ($pos === false) { + return false; + } + + $length = null; + if ($part) { + $length = $pos; + $pos = 0; + } + + return self::substr($haystack, $pos, $length, 'utf-8'); + } + /** * Multibyte safe strlen() function, uses mbstring or iconv for UTF-8, falls back to typo3. *