From: Jan Schneider Date: Mon, 17 Jan 2011 14:12:08 +0000 (+0100) Subject: Add Horde_String::rpos(). X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=30cfb2352091cbac78299fe81e05d18b2d7a78b2;p=horde.git Add Horde_String::rpos(). --- diff --git a/framework/Util/lib/Horde/String.php b/framework/Util/lib/Horde/String.php index 433c1a125..e4941fc15 100644 --- a/framework/Util/lib/Horde/String.php +++ b/framework/Util/lib/Horde/String.php @@ -376,7 +376,8 @@ class Horde_String * * @return integer The position of first occurrence. */ - static public function pos($haystack, $needle, $offset, $charset = 'UTF-8') + static public function pos($haystack, $needle, $offset = 0, + $charset = 'UTF-8') { if (Horde_Util::extensionExists('mbstring')) { $track_errors = ini_set('track_errors', 1); @@ -391,6 +392,34 @@ class Horde_String } /** + * Returns the numeric position of the last occurrence of $needle + * in the $haystack string. + * + * @param string $haystack The string to search through. + * @param string $needle The string to search for. + * @param integer $offset Allows to specify which character in haystack + * to start searching. + * @param string $charset The charset to use when searching for the + * $needle string. + * + * @return integer The position of first occurrence. + */ + static public function rpos($haystack, $needle, $offset = 0, + $charset = 'UTF-8') + { + if (Horde_Util::extensionExists('mbstring')) { + $track_errors = ini_set('track_errors', 1); + $ret = @mb_strrpos($haystack, $needle, $offset, self::_mbstringCharset($charset)); + ini_set('track_errors', $track_errors); + if (!isset($php_errormsg)) { + return $ret; + } + } + + return strrpos($haystack, $needle, $offset); + } + + /** * Returns a string padded to a certain length with another string. * This method behaves exactly like str_pad() but is multibyte safe. * diff --git a/framework/Util/package.xml b/framework/Util/package.xml index b38689c98..06d2fca4e 100644 --- a/framework/Util/package.xml +++ b/framework/Util/package.xml @@ -28,7 +28,9 @@ LGPL +* Added Horde_String::rpos(). * Renamed Horde_Array::array_merge_recursive_overwrite() to Horde_Array::replaceRecursive(). +* Added Horde_String::common(). * Added Horde_Domhtml::. * Removed Horde_Util::assertDriverConfig(). * Removed Horde_Util::bufferOutput(). diff --git a/framework/Util/test/Horde/Util/StringTest.php b/framework/Util/test/Horde/Util/StringTest.php index d035538d6..0c55d216c 100644 --- a/framework/Util/test/Horde/Util/StringTest.php +++ b/framework/Util/test/Horde/Util/StringTest.php @@ -196,6 +196,16 @@ class Horde_Util_StringTest extends PHPUnit_Framework_TestCase Horde_String::length('Я могу есть стекло, оно мне не вредит.', 'utf-8')); } + public function testPos() + { + $this->assertEquals(3, Horde_String::pos('Schöne Neue Welt', 'ö')); + $this->assertEquals(7, Horde_String::pos('Schöne Neue Welt', 'N')); + $this->assertEquals(6, Horde_String::pos('Schöne Neue Welt', ' ')); + $this->assertEquals(3, Horde_String::rpos('Schöne Neue Welt', 'ö')); + $this->assertEquals(7, Horde_String::rpos('Schöne Neue Welt', 'N')); + $this->assertEquals(11, Horde_String::rpos('Schöne Neue Welt', ' ')); + } + public function testPad() { /* Simple single byte tests. */