Skip to content
Permalink
Browse files

MDL-40877 core_text: Added UTF-8 safe strrchr method

  • Loading branch information...
FMCorz committed Aug 16, 2013
1 parent 14e8af6 commit d0720e784ee094d0390ec38799878eaa99f1837b
Showing with 42 additions and 0 deletions.
  1. +12 −0 lib/tests/textlib_test.php
  2. +30 −0 lib/textlib.class.php
@@ -366,6 +366,18 @@ public function test_deprecated_textlib_get_instance() {
$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));
}
}
@@ -238,6 +238,36 @@ public static function substr($text, $start, $len=null, $charset='utf-8') {
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.
*

0 comments on commit d0720e7

Please sign in to comment.
You can’t perform that action at this time.