mirror of
https://github.com/friendica/friendica
synced 2025-05-09 01:44:10 +02:00
Create Plaintext class
create class and move shorten method
This commit is contained in:
parent
c3db62fbcd
commit
fb05570c1d
2 changed files with 39 additions and 30 deletions
37
src/Content/Text/Plaintext.php
Normal file
37
src/Content/Text/Plaintext.php
Normal file
|
@ -0,0 +1,37 @@
|
|||
<?php
|
||||
/**
|
||||
* @file src/Content/Text/Plaintext.php
|
||||
*/
|
||||
namespace Friendica\Content\Text;
|
||||
|
||||
class Plaintext
|
||||
{
|
||||
/**
|
||||
* Shortens message
|
||||
*
|
||||
* @param type $msg
|
||||
* @param type $limit
|
||||
* @return type
|
||||
*
|
||||
* @todo For Twitter URLs aren't shortened, but they have to be calculated as if.
|
||||
*/
|
||||
public static function shorten($msg, $limit)
|
||||
{
|
||||
$lines = explode("\n", $msg);
|
||||
$msg = "";
|
||||
$recycle = html_entity_decode("♲ ", ENT_QUOTES, 'UTF-8');
|
||||
$ellipsis = html_entity_decode("…", ENT_QUOTES, 'UTF-8');
|
||||
foreach ($lines as $row => $line) {
|
||||
if (iconv_strlen(trim($msg . "\n" . $line), "UTF-8") <= $limit) {
|
||||
$msg = trim($msg . "\n" . $line);
|
||||
} elseif (($msg == "") || (($row == 1) && (substr($msg, 0, 4) == $recycle))) {
|
||||
// Is the new message empty by now or is it a reshared message?
|
||||
$msg = iconv_substr(iconv_substr(trim($msg . "\n" . $line), 0, $limit, "UTF-8"), 0, -3, "UTF-8") . $ellipsis;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return $msg;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue