Permalink
Browse files

from ruanyf

  • Loading branch information...
1 parent d14edec commit 894a85581fdcd4d4ca071bcd778f90b82ef88d4b @alexkingorg alexkingorg committed Oct 25, 2012
Showing with 26 additions and 4 deletions.
  1. +26 −4 classes/aktt.php
View
30 classes/aktt.php
@@ -1132,12 +1132,34 @@ static function gmt_to_wp_time($gmt_time) {
}
}
- static function substr_replace($str, $replace, $start, $length) {
- if (function_exists('mb_str_replace')) {
- return mb_substr_replace($str, $replace, $start, $length);
+ static function substr_replace($str, $replace, $start, $length = null) {
+ // from http://www.php.net/manual/en/function.substr-replace.php#90146
+ // via https://github.com/ruanyf/wp-twitter-tools/commit/56d1a4497483b2b39f434fdfab4797d8574088e5
+ if (extension_loaded('mbstring') === true) {
+ $string_length = (is_null($encoding) === true) ? mb_strlen($string) : mb_strlen($string, $encoding);
+
+ if ($start < 0) {
+ $start = max(0, $string_length + $start);
+ }
+ else if ($start > $string_length) {
+ $start = $string_length;
+ }
+ if ($length < 0) {
+ $length = max(0, $string_length - $start + $length);
+ }
+ else if ((is_null($length) === true) || ($length > $string_length)) {
+ $length = $string_length;
+ }
+ if (($start + $length) > $string_length) {
+ $length = $string_length - $start;
+ }
+ if (is_null($encoding) === true) {
+ return mb_substr($string, 0, $start) . $replacement
+ . mb_substr($string, $start + $length, $string_length - $start - $length);
+ }
}
else {
- return substr_replace($str, $replace, $start, $length);
+ return (is_null($length) === true) ? substr_replace($string, $replacement, $start) : substr_replace($string, $replacement, $start, $length);
}
}

0 comments on commit 894a855

Please sign in to comment.