mirror of
https://github.com/smarty-php/smarty.git
synced 2025-08-05 10:54:27 +02:00
fix mb_str_replace to work with utf8
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
===== SVN trunk =====
|
||||
- bugfix mb_str_replace in replace and escape modifiers work with utf8
|
||||
|
||||
31/12/2010
|
||||
- bugfix dynamic configuration of $debugging_crtl did not work
|
||||
- bugfix default value of $config_read_hidden changed to false
|
||||
|
@@ -22,25 +22,6 @@
|
||||
*/
|
||||
function smarty_modifier_escape($string, $esc_type = 'html', $char_set = SMARTY_RESOURCE_CHAR_SET)
|
||||
{
|
||||
if (!function_exists('mb_str_replace')) {
|
||||
// simulate the missing PHP mb_str_replace function
|
||||
function mb_str_replace($needles, $replacements, $haystack)
|
||||
{
|
||||
$rep = (array)$replacements;
|
||||
foreach ((array)$needles as $key => $needle) {
|
||||
$replacement = $rep[$key];
|
||||
$needle_len = mb_strlen($needle);
|
||||
$replacement_len = mb_strlen($replacement);
|
||||
$pos = mb_strpos($haystack, $needle, 0);
|
||||
while ($pos !== false) {
|
||||
$haystack = mb_substr($haystack, 0, $pos) . $replacement
|
||||
. mb_substr($haystack, $pos + $needle_len);
|
||||
$pos = mb_strpos($haystack, $needle, $pos + $replacement_len);
|
||||
}
|
||||
}
|
||||
return $haystack;
|
||||
}
|
||||
}
|
||||
switch ($esc_type) {
|
||||
case 'html':
|
||||
return htmlspecialchars($string, ENT_QUOTES, $char_set);
|
||||
@@ -85,12 +66,8 @@ function smarty_modifier_escape($string, $esc_type = 'html', $char_set = SMARTY_
|
||||
return strtr($string, array('\\' => '\\\\', "'" => "\\'", '"' => '\\"', "\r" => '\\r', "\n" => '\\n', '</' => '<\/'));
|
||||
|
||||
case 'mail':
|
||||
// safe way to display e-mail address on a web page
|
||||
if (function_exists('mb_substr')) {
|
||||
return mb_str_replace(array('@', '.'), array(' [AT] ', ' [DOT] '), $string);
|
||||
} else {
|
||||
return str_replace(array('@', '.'), array(' [AT] ', ' [DOT] '), $string);
|
||||
}
|
||||
require_once(SMARTY_PLUGINS_DIR . 'shared.mb_str_replace.php');
|
||||
return smarty_mb_str_replace(array('@', '.'), array(' [AT] ', ' [DOT] '), $string);
|
||||
|
||||
case 'nonstd':
|
||||
// escape non-standard chars, such as ms document quotes
|
||||
|
@@ -22,30 +22,8 @@
|
||||
*/
|
||||
function smarty_modifier_replace($string, $search, $replace)
|
||||
{
|
||||
if (!function_exists('mb_str_replace')) {
|
||||
// simulate the missing PHP mb_str_replace function
|
||||
function mb_str_replace($needles, $replacements, $haystack)
|
||||
{
|
||||
$rep = (array)$replacements;
|
||||
foreach ((array)$needles as $key => $needle) {
|
||||
$replacement = $rep[$key];
|
||||
$needle_len = mb_strlen($needle);
|
||||
$replacement_len = mb_strlen($replacement);
|
||||
$pos = mb_strpos($haystack, $needle, 0);
|
||||
while ($pos !== false) {
|
||||
$haystack = mb_substr($haystack, 0, $pos) . $replacement
|
||||
. mb_substr($haystack, $pos + $needle_len);
|
||||
$pos = mb_strpos($haystack, $needle, $pos + $replacement_len);
|
||||
}
|
||||
}
|
||||
return $haystack;
|
||||
}
|
||||
}
|
||||
if (function_exists('mb_substr')) {
|
||||
return mb_str_replace($search, $replace, $string);
|
||||
} else {
|
||||
return str_replace($search, $replace, $string);
|
||||
}
|
||||
require_once(SMARTY_PLUGINS_DIR . 'shared.mb_str_replace.php');
|
||||
return smarty_mb_str_replace($search, $replace, $string);
|
||||
}
|
||||
|
||||
?>
|
38
libs/plugins/shared.mb_str_replace.php
Normal file
38
libs/plugins/shared.mb_str_replace.php
Normal file
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
if(!function_exists('smarty_mb_str_replace')) {
|
||||
function smarty_mb_str_replace($search, $replace, $subject, &$count=0) {
|
||||
if (!is_array($search) && is_array($replace)) {
|
||||
return false;
|
||||
}
|
||||
if (is_array($subject)) {
|
||||
// call mb_replace for each single string in $subject
|
||||
foreach ($subject as &$string) {
|
||||
$string = &smarty_mb_str_replace($search, $replace, $string, $c);
|
||||
$count += $c;
|
||||
}
|
||||
} elseif (is_array($search)) {
|
||||
if (!is_array($replace)) {
|
||||
foreach ($search as &$string) {
|
||||
$subject = smarty_mb_str_replace($string, $replace, $subject, $c);
|
||||
$count += $c;
|
||||
}
|
||||
} else {
|
||||
$n = max(count($search), count($replace));
|
||||
while ($n--) {
|
||||
$subject = smarty_mb_str_replace(current($search), current($replace), $subject, $c);
|
||||
$count += $c;
|
||||
next($search);
|
||||
next($replace);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$parts = mb_split(preg_quote($search), $subject);
|
||||
$count = count($parts)-1;
|
||||
$subject = implode($replace, $parts);
|
||||
}
|
||||
return $subject;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
Reference in New Issue
Block a user