Support » Plugin: Click To Tweet » Incorrect handling (length count) of multi-byte strings

  • zachop

    (@zachop)



    The length limit of multi-byte strings (such as those containing greek characters), is incorrectly calculated.

    As a result the tweets are truncated to a shorter length than the one actually set in the plugin’s code (100 chars).

    The issue is resolved by replacing the string handling function calls (strlen, strpos, substr) in the “shorten” function in tm-click-to-tweet.php, line 206, with the multi-byte ones (mb_strlen, mb_strpos, mb_substr respectively).

    Here is the code:

    public function shorten($input, $length, $ellipses = true, $strip_html = true) {
    
        if ($strip_html) {
            $input = strip_tags($input);
        }
        if (mb_strlen($input) <= $length) {
            return $input;
        }
        $last_space = mb_strrpos(mb_substr($input, 0, $length), ' ');
        $trimmed_text = mb_substr($input, 0, $last_space);
        if ($ellipses) {
            $trimmed_text .= '...';
        }
        return $trimmed_text;
    }

    https://wordpress.org/plugins/click-to-tweet-by-todaymade/

Viewing 1 replies (of 1 total)
  • Ben Meredith

    (@benmeredithgmailcom)

    Zachop,

    Thanks for this. In my plugin ( a retool of this one) I’ve now implemented that change. I gave you credit on the changelog page for the fix.

    Thanks!

    Ben Meredith

Viewing 1 replies (of 1 total)
  • The topic ‘Incorrect handling (length count) of multi-byte strings’ is closed to new replies.