I need to shorten a given text (with different encodings!) - eg. to 140 characters - without touching the links.
Example:
Lorem ipsum dolor sit amet: http://bit.ly/111111 Consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat. http://bit.ly/222222 Sed diam voluptua. At vero eos et accusam et justo duo dolores. http://bit.ly/111111
Should end up as:
Lorem ipsum dolor sit amet: http://bit.ly/111111 Consetetur sadipscing elitr, sed diam nonumy... http://bit.ly/222222 http://bit.ly/111111
My actual code with examples is here: http://phpfiddle.org/lite/code/er7-sty
function shortenMessage($message,$limit=140,$encoding='utf-8') {
if (mb_strlen($message,$encoding) <= $limit) return $message;
echo '<pre><h3>Original message:<br />'.$message.'<hr>';
# search positions of links
$reg_exUrl = "/(http|https)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
preg_match_all ($reg_exUrl, $message, $links,PREG_OFFSET_CAPTURE);
echo 'Links found:<br />';
var_dump($links[0]);
echo '<hr>';
$position = array();
$len = 0;
# search utf-8 position of links
foreach ($links[0] as $values) {
$url = $values[0];
$offset = $values[1];
#$pos = mb_strpos($message, $url, $offset, $encoding); # doesnt work
$pos = mb_strpos($message, $url, 0, $encoding);
$position[$pos] = $url;
# delete url from string
$message = str_replace($url, '', $message);
$len += mb_strlen($url,$encoding); # sum lenght of urls to cut from maxlenght
}
echo 'UTF-8 Positions:<br />';
var_dump($position);
echo '<hr>';
# shorten text
$maxlenght = $limit - $len - 7; # 7 is a security buffer
while ($maxlenght < 0) { # too many urls? then cut some...
array_shift($position);
$len -= mb_strlen($position[0],$encoding);
$maxlenght = $limit - $len - 6;
}
echo 'UTF-8 Positions shortened:<br />';
var_dump($position);
echo '<hr>';
$message = mb_substr($message,0,$maxlenght,$encoding).'... ';
echo 'Shortened message without urls:<br />';
var_dump($message);
echo '<hr>';
# re-insert urls at right positions
$addpos = 0;
foreach ($position as $pos => $url) {
$pos += $addpos;
if ($pos < mb_strlen($message,$encoding)) {
$message = mb_substr($message,0,$pos,$encoding).$url.mb_substr($message,$pos,mb_strlen($message),$encoding);
} else {
$message .= ' '.$url;
}
$addpos += mb_strlen($url,$encoding);
}
echo 'Shortened message:<br />';
var_dump($message);
echo '<hr>';
return $message;
}
It works, when there are only different links in the text, but fails, when one link is duplicate.
I've already tried to take the position from preg_match_all as offset for the mb_strpos, but I thinks this fails, because of the preg-match-utf8-problem.
I've seen Shortening text tweet-like without cutting links inside already, but they didn't take care of the encoding and deal with html tags...