Add Horde_String::rpos().
authorJan Schneider <jan@horde.org>
Mon, 17 Jan 2011 14:12:08 +0000 (15:12 +0100)
committerJan Schneider <jan@horde.org>
Mon, 17 Jan 2011 14:14:00 +0000 (15:14 +0100)
framework/Util/lib/Horde/String.php
framework/Util/package.xml
framework/Util/test/Horde/Util/StringTest.php

index 433c1a1..e4941fc 100644 (file)
@@ -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.
      *
index b38689c..06d2fca 100644 (file)
@@ -28,7 +28,9 @@
  </stability>
  <license uri="http://www.gnu.org/copyleft/lesser.html">LGPL</license>
  <notes>
+* 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().
index d035538..0c55d21 100644 (file)
@@ -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. */