Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 4th, 2012  |  syntax: None  |  size: 0.59 KB  |  hits: 8  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. PHP preg_replace shortening a string
  2. $text = preg_replace("/(s)http://([S]+?)(s)/i" ,
  3. "$1[url="http://$2"]http://$2[/url]$3" , " $text ");
  4.        
  5. $text = preg_replace("/(s)http://([S]{,40})([S]*?)(s)/i",
  6. "$1[url="http://$2$3"]http://$2[/url]$4" , " $text ");
  7.        
  8. function replace_links($matches) {
  9.  
  10.     $url = $matches[2];
  11.     $short_url = preg_replace('~^([^/]*)/(.{14})(.{3,})(.{18})$~', '$1/$2...$4', $url);
  12.  
  13.     return $matches[1] . '[url="http://' . $url . '"]http://' . $short_url . '[/url]' . $matches[3];
  14.  
  15. }
  16.  
  17. $text = preg_replace_callback("/(s)http://([S]+?)(s)/i", 'replace_links', " $text ");