]> source.dussan.org Git - nextcloud-server.git/commitdiff
provide multibyte aware helper functions mb_str_replace, mb_substr_replace and mb_arr...
authorArthur Schiwon <blizzz@owncloud.com>
Mon, 2 Jul 2012 18:24:26 +0000 (20:24 +0200)
committerArthur Schiwon <blizzz@owncloud.com>
Mon, 2 Jul 2012 18:31:19 +0000 (20:31 +0200)
lib/helper.php
lib/public/util.php

index b1d2da1452fd066e52de02960e2743f7448b540c..0d18098a4e756ce55d73a2c4a8d6f7f4d2e8cc85 100644 (file)
@@ -41,7 +41,7 @@ class OC_Helper {
                        $app_path = OC_App::getAppPath($app);
                        // Check if the app is in the app folder
                        if( $app_path && file_exists( $app_path.'/'.$file )){
-                               if(substr($file, -3) == 'php' || substr($file, -3) == 'css'){   
+                               if(substr($file, -3) == 'php' || substr($file, -3) == 'css'){
                                        $urlLinkTo =  OC::$WEBROOT . '/?app=' . $app;
                                        $urlLinkTo .= ($file!='index.php')?'&getfile=' . urlencode($file):'';
                                }else{
@@ -379,7 +379,7 @@ class OC_Helper {
                        //trim the character set from the end of the response
                        $mimeType=substr($reply,0,strrpos($reply,' '));
 
-                       //trim ; 
+                       //trim ;
                        if (strpos($mimeType, ';') !== false) {
                                $mimeType = strstr($mimeType, ';', true);
                        }
@@ -586,11 +586,11 @@ class OC_Helper {
 
                return $newpath;
        }
-       
+
        /*
         * checks if $sub is a subdirectory of $parent
-        * 
-        * @param $sub 
+        *
+        * @param $sub
         * @param $parent
         * @return bool
         */
@@ -620,4 +620,68 @@ class OC_Helper {
                exit;*/
                return false;
        }
+
+       /**
+       * @brief Returns an array with all keys from input lowercased or uppercased. Numbered indices are left as is.
+       *
+       * @param $input The array to work on
+       * @param $case Either MB_CASE_UPPER or MB_CASE_LOWER (default)
+       * @param $encoding The encoding parameter is the character encoding. Defaults to UTF-8
+       * @return array
+       *
+       * Returns an array with all keys from input lowercased or uppercased. Numbered indices are left as is.
+       * based on http://www.php.net/manual/en/function.array-change-key-case.php#107715
+       *
+       */
+       public static function mb_array_change_key_case($input, $case = MB_CASE_LOWER, $encoding = 'UTF-8'){
+               $case = ($case != MB_CASE_UPPER) ? MB_CASE_LOWER : MB_CASE_UPPER;
+               $ret = array();
+               foreach ($input as $k => $v) {
+                       $ret[mb_convert_case($k, $case, $encoding)] = $v;
+               }
+               return $ret;
+       }
+
+       /**
+       * @brief replaces a copy of string delimited by the start and (optionally) length parameters with the string given in replacement.
+       *
+       * @param $input The input string. .Opposite to the PHP build-in function does not accept an array.
+       * @param $replacement The replacement string.
+       * @param $start If start is positive, the replacing will begin at the start'th offset into string. If start is negative, the replacing will begin at the start'th character from the end of string.
+       * @param $length Length of the part to be replaced
+       * @param $encoding The encoding parameter is the character encoding. Defaults to UTF-8
+       * @return string
+       *
+       */
+       public static function mb_substr_replace($string, $replacement, $start, $length = null, $encoding = 'UTF-8') {
+               $start = intval($start);
+               $length = intval($length);
+               $string = mb_substr($string, 0, $start, $encoding) .
+                         $replacement .
+                         mb_substr($string, $start+$length, mb_strlen($string, 'UTF-8')-$start, $encoding);
+
+               return $string;
+       }
+
+       /**
+       * @brief Replace all occurrences of the search string with the replacement string
+       *
+       * @param $search The value being searched for, otherwise known as the needle. String.
+       * @param $replace The replacement string.
+       * @param $subject The string or array being searched and replaced on, otherwise known as the haystack.
+       * @param $encoding The encoding parameter is the character encoding. Defaults to UTF-8
+       * @param $count If passed, this will be set to the number of replacements performed.
+       * @return string
+       *
+       */
+       public static function mb_str_replace($search, $replace, $subject, $encoding = 'UTF-8', &$count = null) {
+               $offset = -1;
+               $length = mb_strlen($search, 'UTF-8');
+               while(($i = mb_strrpos($subject, $search, $offset, 'UTF-8'))) {
+                       $subject = OC_Helper::mb_substr_replace($subject, $replace, $i, $length);
+                       $offset = $i - mb_strlen($subject, 'UTF-8') - 1;
+                       $count++;
+               }
+               return $subject;
+       }
 }
index c611d59a533e4f9a3e12e245a2748b6369c05c7d..41121091544abafecacbedfd9b058de98e00e7db 100644 (file)
@@ -26,7 +26,7 @@
  *
  */
 
-// use OCP namespace for all classes that are considered public. 
+// use OCP namespace for all classes that are considered public.
 // This means that they should be used by apps instead of the internal ownCloud classes
 namespace OCP;
 
@@ -54,7 +54,7 @@ class Util {
 
 
        /**
-        * @brief send an email 
+        * @brief send an email
         * @param string $toaddress
         * @param string $toname
         * @param string $subject
@@ -264,17 +264,61 @@ class Util {
        public static function callCheck(){
                return(\OC_Util::callCheck());
        }
-       
-       /**\r
-        * @brief Used to sanitize HTML\r
-        *\r
-        * This function is used to sanitize HTML and should be applied on any string or array of strings before displaying it on a web page.\r
-        *\r
-        * @param string or array of strings\r
-        * @return array with sanitized strings or a single sinitized string, depends on the input parameter.\r
-        */\r
-       public static function sanitizeHTML( $value ){\r
-               return(\OC_Util::sanitizeHTML($value));\r
+
+       /**
+        * @brief Used to sanitize HTML
+        *
+        * This function is used to sanitize HTML and should be applied on any string or array of strings before displaying it on a web page.
+        *
+        * @param string or array of strings
+        * @return array with sanitized strings or a single sinitized string, depends on the input parameter.
+        */
+       public static function sanitizeHTML( $value ){
+               return(\OC_Util::sanitizeHTML($value));
+       }
+
+       /**
+       * @brief Returns an array with all keys from input lowercased or uppercased. Numbered indices are left as is.
+       *
+       * @param $input The array to work on
+       * @param $case Either MB_CASE_UPPER or MB_CASE_LOWER (default)
+       * @param $encoding The encoding parameter is the character encoding. Defaults to UTF-8
+       * @return array
+       *
+       *
+       */
+       public static function mb_array_change_key_case($input, $case = MB_CASE_LOWER, $encoding = 'UTF-8'){
+               return(\OC_Helper::mb_array_change_key_case($input, $case, $encoding));
+       }
+
+       /**
+       * @brief replaces a copy of string delimited by the start and (optionally) length parameters with the string given in replacement.
+       *
+       * @param $input The input string. .Opposite to the PHP build-in function does not accept an array.
+       * @param $replacement The replacement string.
+       * @param $start If start is positive, the replacing will begin at the start'th offset into string. If start is negative, the replacing will begin at the start'th character from the end of string.
+       * @param $length Length of the part to be replaced
+       * @param $encoding The encoding parameter is the character encoding. Defaults to UTF-8
+       * @return string
+       *
+       */
+       public static function mb_substr_replace($string, $replacement, $start, $length = null, $encoding = 'UTF-8') {
+               return(\OC_Helper::mb_substr_replace($string, $replacement, $start, $length, $encoding));
+       }
+
+       /**
+       * @brief Replace all occurrences of the search string with the replacement string
+       *
+       * @param $search The value being searched for, otherwise known as the needle. String.
+       * @param $replace The replacement string.
+       * @param $subject The string or array being searched and replaced on, otherwise known as the haystack.
+       * @param $encoding The encoding parameter is the character encoding. Defaults to UTF-8
+       * @param $count If passed, this will be set to the number of replacements performed.
+       * @return string
+       *
+       */
+       public static function mb_str_replace($search, $replace, $subject, $encoding = 'UTF-8', &$count = null) {
+               return(\OC_Helper::mb_str_replace($search, $replace, $subject, $encoding, $count));
        }
 }