All pastes #962970 Raw Edit

Mine

public text v1 · immutable
#962970 ·published 2008-03-30 03:58 UTC
rendered paste body
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]}<a href=\"{$matches[3]}\" target=\"_blank\">$url</a> {$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]}<a href=\"http://{$matches[3]}\" target=\"_blank\">$url</a> {$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]}<a href=\"mailto:{$matches[3]}@{$matches[4]}\" target=\"_blank\">$url</a> {$matches[4]}";
        }
    }
    $content = trim($content);
    return $content;
}