
Untitled
By: a guest on
Jul 4th, 2012 | syntax:
None | size: 0.59 KB | hits: 8 | expires: Never
PHP preg_replace shortening a string
$text = preg_replace("/(s)http://([S]+?)(s)/i" ,
"$1[url="http://$2"]http://$2[/url]$3" , " $text ");
$text = preg_replace("/(s)http://([S]{,40})([S]*?)(s)/i",
"$1[url="http://$2$3"]http://$2[/url]$4" , " $text ");
function replace_links($matches) {
$url = $matches[2];
$short_url = preg_replace('~^([^/]*)/(.{14})(.{3,})(.{18})$~', '$1/$2...$4', $url);
return $matches[1] . '[url="http://' . $url . '"]http://' . $short_url . '[/url]' . $matches[3];
}
$text = preg_replace_callback("/(s)http://([S]+?)(s)/i", 'replace_links', " $text ");