]> git.mxchange.org Git - friendica.git/blob - src/Content/Text/Plaintext.php
Merge pull request #4345 from annando/bugfix-oembed
[friendica.git] / src / Content / Text / Plaintext.php
1 <?php
2 /**
3  * @file src/Content/Text/Plaintext.php
4  */
5 namespace Friendica\Content\Text;
6
7 class Plaintext
8 {
9         /**
10          * Shortens message
11          *
12          * @param type $msg
13          * @param type $limit
14          * @return type
15          *
16          * @todo For Twitter URLs aren't shortened, but they have to be calculated as if.
17          */
18         public static function shorten($msg, $limit)
19         {
20                 $lines = explode("\n", $msg);
21                 $msg = "";
22                 $recycle = html_entity_decode("&#x2672; ", ENT_QUOTES, 'UTF-8');
23                 $ellipsis = html_entity_decode("&#x2026;", ENT_QUOTES, 'UTF-8');
24                 foreach ($lines as $row => $line) {
25                         if (iconv_strlen(trim($msg . "\n" . $line), "UTF-8") <= $limit) {
26                                 $msg = trim($msg . "\n" . $line);
27                         } elseif (($msg == "") || (($row == 1) && (substr($msg, 0, 4) == $recycle))) {
28                                 // Is the new message empty by now or is it a reshared message?
29                                 $msg = iconv_substr(iconv_substr(trim($msg . "\n" . $line), 0, $limit, "UTF-8"), 0, -3, "UTF-8") . $ellipsis;
30                         } else {
31                                 break;
32                         }
33                 }
34
35                 return $msg;
36         }
37 }