
February 25th, 2013, 12:00 PM
|
 |
Still alive
|
|
Join Date: Mar 2007
Location: Washington, USA
|
|
Use preg_replace_callback() and make the callback function shorten $3 before returning the new string. Like
PHP Code:
function replace_sulr($str) {
$pattern = '#(^|[^\"=]{1})(http://|ftp://|mailto:|news:)([^\s<>]+)([\s\n<>]|$)#sm';
return preg_replace_callback($pattern, function($match) {
$format = "%s<a href=\"%s%s\" target=\"_blank\" class=\"crmnewsdetail_link\"><span class=\"crmnewdetail_linq\"> </span>%s</a>%s";
list(, $before, $protocol, $url, $after) = $match;
if (strlen($url) > 50) {
$shorturl = substr($url, 0, 10) . "..." . substr($url, -20);
} else {
$shorturl = $url;
}
return sprintf($format, $before, $protocol, $url, $shorturl, $after);
}, $str);
}
|