| 148 |
lars |
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 |
* Multibyte string replace
|
|
|
11 |
*
|
|
|
12 |
* @param string|string[] $search the string to be searched
|
|
|
13 |
* @param string|string[] $replace the replacement string
|
|
|
14 |
* @param string $subject the source string
|
|
|
15 |
* @param int &$count number of matches found
|
|
|
16 |
*
|
|
|
17 |
* @return string replaced string
|
|
|
18 |
* @author Rodney Rehm
|
|
|
19 |
*/
|
|
|
20 |
function smarty_mb_str_replace($search, $replace, $subject, &$count = 0)
|
|
|
21 |
{
|
|
|
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 |
$mb_reg_charset = mb_regex_encoding();
|
|
|
48 |
// Check if mbstring regex is using UTF-8
|
|
|
49 |
$reg_is_unicode = !strcasecmp($mb_reg_charset, "UTF-8");
|
|
|
50 |
if(!$reg_is_unicode) {
|
|
|
51 |
// ...and set to UTF-8 if not
|
|
|
52 |
mb_regex_encoding("UTF-8");
|
|
|
53 |
}
|
|
|
54 |
|
|
|
55 |
// See if charset used by Smarty is matching one used by regex...
|
|
|
56 |
$current_charset = mb_regex_encoding();
|
|
|
57 |
$convert_result = (bool)strcasecmp(Smarty::$_CHARSET, $current_charset);
|
|
|
58 |
if($convert_result) {
|
|
|
59 |
// ...convert to it if not.
|
|
|
60 |
$subject = mb_convert_encoding($subject, $current_charset, Smarty::$_CHARSET);
|
|
|
61 |
$search = mb_convert_encoding($search, $current_charset, Smarty::$_CHARSET);
|
|
|
62 |
$replace = mb_convert_encoding($replace, $current_charset, Smarty::$_CHARSET);
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
$parts = mb_split(preg_quote($search), $subject ?? "") ?: array();
|
|
|
66 |
// If original regex encoding was not unicode...
|
|
|
67 |
if(!$reg_is_unicode) {
|
|
|
68 |
// ...restore original regex encoding to avoid breaking the system.
|
|
|
69 |
mb_regex_encoding($mb_reg_charset);
|
|
|
70 |
}
|
|
|
71 |
if($parts === false) {
|
|
|
72 |
// This exception is thrown if call to mb_split failed.
|
|
|
73 |
// Usually it happens, when $search or $replace are not valid for given mb_regex_encoding().
|
|
|
74 |
// There may be other cases for it to fail, please file an issue if you find a reproducible one.
|
|
|
75 |
throw new SmartyException("Source string is not a valid $current_charset sequence (probably)");
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
$count = count($parts) - 1;
|
|
|
79 |
$subject = implode($replace, $parts);
|
|
|
80 |
// Convert results back to charset used by Smarty, if needed.
|
|
|
81 |
if($convert_result) {
|
|
|
82 |
$subject = mb_convert_encoding($subject, Smarty::$_CHARSET, $current_charset);
|
|
|
83 |
}
|
|
|
84 |
}
|
|
|
85 |
return $subject;
|
|
|
86 |
}
|
|
|
87 |
}
|