]> git.mxchange.org Git - friendica.git/blob - library/Smarty/libs/plugins/shared.mb_str_replace.php
Add Smarty to Composer
[friendica.git] / library / Smarty / libs / plugins / shared.mb_str_replace.php
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     {
23         if (!is_array($search) && is_array($replace)) {
24             return false;
25         }
26         if (is_array($subject)) {
27             // call mb_replace for each single string in $subject
28             foreach ($subject as &$string) {
29                 $string = & smarty_mb_str_replace($search, $replace, $string, $c);
30                 $count += $c;
31             }
32         } elseif (is_array($search)) {
33             if (!is_array($replace)) {
34                 foreach ($search as &$string) {
35                     $subject = smarty_mb_str_replace($string, $replace, $subject, $c);
36                     $count += $c;
37                 }
38             } else {
39                 $n = max(count($search), count($replace));
40                 while ($n --) {
41                     $subject = smarty_mb_str_replace(current($search), current($replace), $subject, $c);
42                     $count += $c;
43                     next($search);
44                     next($replace);
45                 }
46             }
47         } else {
48             $parts = mb_split(preg_quote($search), $subject);
49             $count = count($parts) - 1;
50             $subject = implode($replace, $parts);
51         }
52
53         return $subject;
54     }
55 }