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

Untitled

By: a guest on Jun 4th, 2012  |  syntax: None  |  size: 2.54 KB  |  hits: 9  |  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. RegEx to convert URLs in text into clickable ones with custom anchor text [closed]
  2. http://www.website.com/1/
  3. Click here http://www.website.com/2/ or visit the website: http://www.website.com/3/
  4. or http://www.website.com/4/
  5. http://www.website.com/5/
  6.        
  7. <a href="http://www.website.com/1/">http://www.website.com/1/</a>
  8. Click <a href="http://www.website.com/2/">here</a> or visit the website: <a href="http://www.website.com/3/">http://www.website.com/3/</a>
  9. or <a href="http://www.website.com/4/">http://www.website.com/4/</a>
  10. <a href="http://www.website.com/5/">http://www.website.com/5/</a>
  11.        
  12. preg_replace("#(^|[n ])([w]+?://[w#$%&~/.-;:=,?@[]+]*)#is", "\1<a    href="\2" target="_blank">\2</a>", $ret);
  13. //                     ^---- I've tried adding "|here "
  14. //                           But I cannot get the order of \1 and \2 right
  15.        
  16. (?:(here)s*|b)(w+?://[w#$%&~/.-;:=,?@[]+]*)
  17.        
  18. <a href="$2">$2</a>
  19.        
  20. <a href="$2">$1</a>
  21.        
  22. class MakeItLink {
  23.     protected function _link_www( $matches ) {
  24.         $url = $matches[2];
  25.         $url = MakeItLink::cleanURL( $url );
  26.         if( empty( $url ) ) {
  27.             return $matches[0];
  28.         }
  29.  
  30.         return "{$matches[1]}<a href='{$url}'>{$url}</a>";
  31.     }
  32.  
  33.     public function cleanURL( $url ) {
  34.         if( $url == '' ) {
  35.             return $url;
  36.         }
  37.  
  38.         $url = preg_replace( "|[^a-z0-9-~+_.?#=!&;,/:%@$*'()x80-xff]|i", '', $url );
  39.         $url = str_replace( array( "%0d", "%0a" ), '', $url );
  40.         $url = str_replace( ";//", "://", $url );
  41.  
  42.         /* If the URL doesn't appear to contain a scheme, we
  43.          * presume it needs http:// appended (unless a relative
  44.          * link starting with / or a php file).
  45.          */
  46.         if(
  47.             strpos( $url, ":" ) === false
  48.             && substr( $url, 0, 1 ) != "/"
  49.             && !preg_match( "|^[a-z0-9-]+?.php|i", $url )
  50.         ) {
  51.             $url = "http://{$url}";
  52.         }
  53.  
  54.         // Replace ampersans and single quotes
  55.         $url = preg_replace( "|&([^#])(?![a-z]{2,8};)|", "&#038;$1", $url );
  56.         $url = str_replace( "'", "&#039;", $url );
  57.  
  58.         return $url;
  59.     }
  60.  
  61.     public function transform( $text ) {
  62.         $text = " {$text}";
  63.  
  64.         $text = preg_replace_callback(
  65.             '#(?])(()?([w]+?://(?:[w\x80-\xff#$%&~/-=?@[](+]|[.,;:](?![s<])|(?(1))(?![s<])|)))*)#is&#039;,
  66.             array( &#039;MakeItLink&#039;, &#039;_link_www&#039; ),
  67.             $text
  68.         );
  69.  
  70.         $text = preg_replace( &#039;#(<a>]+?>|>))<a>]+?>([^>]+?)</a></a>#i', "$1$3</a>", $text );
  71.         $text = trim( $text );
  72.  
  73.         return $text;
  74.     }
  75. }