library/smarty/libs/plugins/shared.mb_str_replace.php
changeset 46 f11c31f7fa3e
parent 45 a56e7f9a0463
child 47 03388ec805b4
equal deleted inserted replaced
45:a56e7f9a0463 46:f11c31f7fa3e
     1 <?php
       
     2 /**
       
     3  * Smarty shared plugin
       
     4  *
       
     5  * @package    Smarty
       
     6  * @subpackage PluginsShared
       
     7  */
       
     8 if (!function_exists('smarty_mb_str_replace')) {
       
     9 
       
    10     /**
       
    11      * Multibyte string replace
       
    12      *
       
    13      * @param  string $search the string to be searched
       
    14      * @param  string $replace the replacement string
       
    15      * @param  string $subject the source string
       
    16      * @param  int &$count number of matches found
       
    17      *
       
    18      * @return string replaced string
       
    19      * @author Rodney Rehm
       
    20      */
       
    21     function smarty_mb_str_replace($search, $replace, $subject, &$count = 0) {
       
    22         if (!is_array($search) && is_array($replace)) {
       
    23             return false;
       
    24         }
       
    25         if (is_array($subject)) {
       
    26             // call mb_replace for each single string in $subject
       
    27             foreach ($subject as &$string) {
       
    28                 $string = &smarty_mb_str_replace($search, $replace, $string, $c);
       
    29                 $count += $c;
       
    30             }
       
    31         } elseif (is_array($search)) {
       
    32             if (!is_array($replace)) {
       
    33                 foreach ($search as &$string) {
       
    34                     $subject = smarty_mb_str_replace($string, $replace, $subject, $c);
       
    35                     $count += $c;
       
    36                 }
       
    37             } else {
       
    38                 $n = max(count($search), count($replace));
       
    39                 while ($n--) {
       
    40                     $subject = smarty_mb_str_replace(current($search), current($replace), $subject, $c);
       
    41                     $count += $c;
       
    42                     next($search);
       
    43                     next($replace);
       
    44                 }
       
    45             }
       
    46         } else {
       
    47             $parts = mb_split(preg_quote($search), $subject);
       
    48             $count = count($parts) - 1;
       
    49             $subject = implode($replace, $parts);
       
    50         }
       
    51 
       
    52         return $subject;
       
    53     }
       
    54 }