Permalink
Browse files

Merge branch 'feature/mb-encoding' into develop

  • Loading branch information...
2 parents f50bc8a + 37e8f04 commit 8cac586b8080a1d9ff1c650344725675a9fc3fc7 @alexkingorg alexkingorg committed Oct 25, 2012
Showing with 68 additions and 12 deletions.
  1. +58 −2 classes/aktt.php
  2. +10 −10 classes/aktt_tweet.php
View
60 classes/aktt.php
@@ -1052,7 +1052,7 @@ static function profile_link($username) {
}
static function profile_prefix($username, $prefix = '@') {
- if (substr($username, 0, 1) != '#') {
+ if (AKTT::substr($username, 0, 1) != '#') {
$username = '@'.$username;
}
return $username;
@@ -1069,7 +1069,7 @@ static function hashtag_link($hashtag) {
}
static function hashtag_prefix($hashtag, $prefix = '#') {
- if (substr($hashtag, 0, 1) != '#') {
+ if (AKTT::substr($hashtag, 0, 1) != '#') {
$hashtag = '#'.$hashtag;
}
return $hashtag;
@@ -1132,6 +1132,62 @@ static function gmt_to_wp_time($gmt_time) {
}
}
+ static function substr_replace($string, $replacement, $start, $length = null, $encoding = 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);
+ }
+ return mb_substr($string, 0, $start, $encoding) . $replacement
+ . mb_substr($string, $start + $length, $string_length - $start - $length, $encoding);
+ }
+ else {
+ return (is_null($length) === true) ? substr_replace($string, $replacement, $start) : substr_replace($string, $replacement, $start, $length);
+ }
+ }
+
+ static function strlen($str, $encoding = null) {
+ if (function_exists('mb_strlen')) {
+ if (is_null($encoding) === true) {
+ return mb_strlen($str);
+ }
+ else {
+ return mb_strlen($str, $encoding);
+ }
+ }
+ else {
+ return strlen($str);
+ }
+ }
+
+ static function substr($str, $start, $length) {
+ if (function_exists('mb_substr')) {
+ return mb_substr($str, $start, $length);
+ }
+ else {
+ return substr($str, $start, $length);
+ }
+ }
+
}
View
20 classes/aktt_tweet.php
@@ -73,8 +73,8 @@ public function id() {
*/
public function title() {
if (isset($this->data)) {
- $title = trim(substr($this->data->text, 0, 50));
- if (strlen($this->data->text) > 50) {
+ $title = trim(AKTT::substr($this->data->text, 0, 50));
+ if (AKTT::strlen($this->data->text) > 50) {
$title = $title.'...';
}
}
@@ -282,7 +282,7 @@ function get_post($post_type = null) {
* @return bool
*/
function is_reply() {
- return (bool) (substr($this->content(), 0, 1) == '@' || !empty($this->data->in_reply_to_screen_name));
+ return (bool) (AKTT::substr($this->content(), 0, 1) == '@' || !empty($this->data->in_reply_to_screen_name));
}
@@ -292,7 +292,7 @@ function is_reply() {
* @return bool
*/
function is_retweet() {
- return (bool) (substr($this->content(), 0, 2) == 'RT' || !empty($this->data->retweeted_status));
+ return (bool) (AKTT::substr($this->content(), 0, 2) == 'RT' || !empty($this->data->retweeted_status));
}
@@ -353,18 +353,18 @@ function link_entities($defer_to_anywhere = true) {
// $log = array();
// $log[] = 'diff: '.$diff;
// $log[] = 'entity start: '.$entity['start'];
-// $log[] = 'entity start chars: '.substr($this->content(), $entity['start'], 3);
+// $log[] = 'entity start chars: '.AKTT::substr($this->content(), $entity['start'], 3);
// $log[] = 'diff start: '.$start;
-// $log[] = 'diff start chars: '.substr($str, $start, 3);
+// $log[] = 'diff start chars: '.AKTT::substr($str, $start, 3);
// $log[] = 'entity end: '.$entity['end'];
// $log[] = 'diff end: '.$end;
-// $log[] = 'find len: '.strlen($entity['find']);
+// $log[] = 'find len: '.AKTT::strlen($entity['find']);
// $log[] = 'find: '.htmlspecialchars($entity['find']);
-// $log[] = 'replace len: '.strlen($entity['replace']);
+// $log[] = 'replace len: '.AKTT::strlen($entity['replace']);
// $log[] = 'replace: '.htmlspecialchars($entity['replace']);
// echo '<p>'.implode('<br>', $log).'</p>';
- $str = substr_replace($str, $entity['replace'], $start, ($end - $start));
- $diff += strlen($entity['replace']) - ($end - $start);
+ $str = AKTT::substr_replace($str, $entity['replace'], $start, ($end - $start));
+ $diff += AKTT::strlen($entity['replace']) - ($end - $start);
}
return $str;
}

0 comments on commit 8cac586

Please sign in to comment.