function shorten($content,$type) {
if ($type == 'http') {
$pattern = "#(.*)((?:^|[\n ]))([\w]+?://[^ \"\n\r\t<]*)(.*)#is";// we define same pattern as above
if (preg_match($pattern, $content, $matches)){ // we simply get the results instead of replacing at the same time
$url = $matches[3];
if (strlen($url)>20) { // check length
$url= substr($url,0,20) . "...";
}
$prefix = $this->shorten($matches[1], 'http');
$content = "$prefix{$matches[2]}$url {$matches[4]}";
}
} else if ($type == 'www') {
$pattern = "#(.*)((?:^|[\n ]))((?:www|ftp)\.[^ \"\t\n\r<]*)(.*)#is";// we define same pattern as above
if (preg_match($pattern, $content, $matches)){ // we simply get the results instead of replacing at the same time
$url = $matches[3];
if (strlen($url)>20) { // check length
$url= substr($url,0,20) . "...";
}
$prefix = $this->shorten($matches[1], 'www');
$content = "$prefix{$matches[2]}$url {$matches[4]}";
}
} else if ($type == 'mail') {
$pattern = "#(.*)((?:^|[\n ]))([a-z0-9&\-_.]+?)@([\w\-]+\.(?:[\w\-\.]+\.)*[\w]+)(.*)#is";// we define same pattern as above
if (preg_match($pattern, $content, $matches)){ // we simply get the results instead of replacing at the same time
$url = "{$matches[3]}@{$matches[4]}";
if (strlen($url)>20) { // check length
$url= substr($url,0,20) . "...";
}
$prefix = $this->shorten($matches[1], 'mail');
$content = "$prefix{$matches[2]}$url {$matches[4]}";
}
}
$content = trim($content);
return $content;
}