]> gitweb.fluxo.info Git - lorea/elgg.git/commitdiff
Wrapped all multibyte functions that are directly interchangable with non mb function...
authorbrettp <brettp@36083f99-b078-4883-b0ff-0f9b5a30f544>
Thu, 12 Nov 2009 04:01:52 +0000 (04:01 +0000)
committerbrettp <brettp@36083f99-b078-4883-b0ff-0f9b5a30f544>
Thu, 12 Nov 2009 04:01:52 +0000 (04:01 +0000)
git-svn-id: http://code.elgg.org/elgg/trunk@3670 36083f99-b078-4883-b0ff-0f9b5a30f544

engine/lib/mb_wrapper.php

index 2cef15c64da129906d5911280934340e8f6209a0..7d49da315bba36b9de8e42ce45ba36743ffb130c 100644 (file)
@@ -1,61 +1,44 @@
 <?php
-/**
- * Elgg wrapper functions for multibyte string support.
- *
- * @package Elgg
- * @subpackage Core
- * @author Curverider Ltd
- * @link http://elgg.org/
- */
 
-/**
- * Wrapper function: Returns the result of mb_strtolower if mb_support is present, else the
- * result of strtolower is returned.
- *
- * @param string $string The string.
- * @param string $charset The charset (if multibyte support is present) : default 'UTF8'
- * @return string
- */
-function elgg_strtolower($string, $charset = 'UTF8') {
-       if (is_callable('mb_strtolower')) {
-               return mb_strtolower($string, $charset);
-       }
-
-       return strtolower($string);
+// if mb functions are available, set internal encoding to UTF8
+if (is_callable('mb_internal_encoding')) {
+       mb_internal_encoding("UTF-8");
 }
 
-/**
- * Wrapper function: Returns the result of mb_strtoupper if mb_support is present, else the
- * result of strtoupper is returned.
- *
- * @param string $string The string.
- * @param string $charset The charset (if multibyte support is present) : default 'UTF8'
- * @return string
- */
-function elgg_strtoupper($string, $charset = 'UTF8') {
-       if (is_callable('mb_strtoupper')) {
-               return mb_strtoupper($string, $charset);
-       }
+// map string functions to their mb_str_func alternatives
+// and wrap them in elgg_str_fun()
 
-       return strtoupper($string);
-}
+// list of non-mb safe string functions to wrap in elgg_*()
+// only will work with mb_* functions that take the same
+// params in the same order as their non-mb safe counterparts.
+$str_funcs = array(
+       'parse_str',
+       'split',
+       'stristr',
+       'strlen',
+       'strpos',
+       'strrchr',
+       'strripos',
+       'strrpos',
+       'strstr',
+       'strtolower',
+       'strtoupper',
+       'substr_count',
+       'substr'
+);
 
-/**
- * Wrapper function: Returns the result of mb_substr if mb_support is present, else the
- * result of substr is returned.
- *
- * @param string $string The string.
- * @param int $start Start position.
- * @param int $length Length.
- * @param string $charset The charset (if multibyte support is present) : default 'UTF8'
- * @return string
- */
-function elgg_substr($string, $start = 0, $length = null, $charset = 'UTF8') {
-       if (is_callable('mb_substr')) {
-               return mb_substr($string, $start, $length, $charset);
+$eval_statement = '';
+foreach ($str_funcs as $func) {
+       // create wrapper function passing in the same args as given
+       $eval_statement .= "
+       function elgg_$func() {
+               \$args = func_get_args();
+               if (is_callable('$mb_func')) {
+                       return call_user_func_array('$mb_func', \$args);
+               }
+               return call_user_func_array('$func', \$args);
        }
-
-       return substr($string, $start, $length);
+";
 }
 
 // TODO: Other wrapper functions
\ No newline at end of file