Browse Source

provide multibyte aware helper functions mb_str_replace, mb_substr_replace and mb_array_change_key_case for handling with UTF 8

tags/v4.5.0beta1
Arthur Schiwon 12 years ago
parent
commit
52822652bc
2 changed files with 126 additions and 18 deletions
  1. 69
    5
      lib/helper.php
  2. 57
    13
      lib/public/util.php

+ 69
- 5
lib/helper.php View 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;
}
}

+ 57
- 13
lib/public/util.php View 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());
}
/**
* @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 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));
}
}


Loading…
Cancel
Save