Browse Source

[ZF-7023] Zend_Filter_StrimTrim:

 - added unicode awareness for stringtrim filter
   (PHP does not support this)

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@16191 44c647ce-9c0f-0410-b52a-842ac1e357ba
thomas 16 years ago
parent
commit
8e6742dca3
2 changed files with 22 additions and 8 deletions
  1. 22 5
      library/Zend/Filter/StringTrim.php
  2. 0 3
      tests/Zend/Filter/StringTrimTest.php

+ 22 - 5
library/Zend/Filter/StringTrim.php

@@ -1,5 +1,4 @@
 <?php
-
 /**
  * Zend Framework
  *
@@ -20,13 +19,11 @@
  * @version    $Id$
  */
 
-
 /**
  * @see Zend_Filter_Interface
  */
 require_once 'Zend/Filter/Interface.php';
 
-
 /**
  * @category   Zend
  * @package    Zend_Filter
@@ -89,9 +86,29 @@ class Zend_Filter_StringTrim implements Zend_Filter_Interface
     public function filter($value)
     {
         if (null === $this->_charList) {
-            return trim((string) $value);
+            return $this->_unicodeTrim((string) $value);
         } else {
-            return trim((string) $value, $this->_charList);
+            return $this->_unicodeTrim((string) $value, $this->_charList);
         }
     }
+
+    /**
+     * Unicode aware trim method
+     * Fixes a PHP problem
+     *
+     * @param string $value
+     * @param string $charlist
+     * @return string
+     */
+    protected function _unicodeTrim($value, $charlist = '\\\\s')
+    {
+        $chars = preg_replace(
+            array( '/[\^\-\]\\\]/S', '/\\\{4}/S' ),
+            array( '\\\\\\0', '\\' ),
+            $charlist
+        );
+
+        $pattern = '^[' . $chars . ']*|[' . $chars . ']*$';
+        return preg_replace("/$pattern/usSD", '', $value);
+    }
 }

+ 0 - 3
tests/Zend/Filter/StringTrimTest.php

@@ -1,5 +1,4 @@
 <?php
-
 /**
  * Zend Framework
  *
@@ -21,7 +20,6 @@
  * @version    $Id$
  */
 
-
 /**
  * Test helper
  */
@@ -32,7 +30,6 @@ require_once dirname(__FILE__) . '/../../TestHelper.php';
  */
 require_once 'Zend/Filter/StringTrim.php';
 
-
 /**
  * @category   Zend
  * @package    Zend_Filter