[HOW TO] Fix for long url's and link opens in new window

How-to's, little tricks, tutorials, code examples (snippets) and read-me's.

[HOW TO] Fix for long url's and link opens in new window

Postby installspark » 15 Jul 2007, 08:50

This stops url's over 39 characters being being shortened to http://www.yoursite.com/yourforum/anyth ... ything.php

Open includes/functions.php

Find
Code: Select all
case MAGIC_URL_FULL:
         $tag   = 'm';
         $text   = (strlen($url) > 55) ? substr($url, 0, 39) . ' ... ' . substr($url, -10) : $url;
      break;

      case MAGIC_URL_WWW:
         $tag   = 'w';
         $url   = 'http://' . $url;
         $text   = (strlen($url) > 55) ? substr($url, 0, 39) . ' ... ' . substr($url, -10) : $url;
      break;

      case MAGIC_URL_EMAIL:
         $tag   = 'e';
         $text   = (strlen($url) > 55) ? substr($url, 0, 39) . ' ... ' . substr($url, -10) : $url;
         $url   = 'mailto:' . $url;
      break;
   }


Replace
Code: Select all
case MAGIC_URL_FULL:
         $tag   = 'm';
         $text   = (strlen($url) > 100) ? substr($url, 0, 100) . ' ... ' . substr($url, -10) : $url;
      break;

      case MAGIC_URL_WWW:
         $tag   = 'w';
         $url   = 'http://' . $url;
         $text   = (strlen($url) > 100) ? substr($url, 0, 100) . ' ... ' . substr($url, -10) : $url;
      break;

      case MAGIC_URL_EMAIL:
         $tag   = 'e';
         $text   = (strlen($url) > 100) ? substr($url, 0, 100) . ' ... ' . substr($url, -10) : $url;
         $url   = 'mailto:' . $url;
      break;
   }


Find
Code: Select all
$magic_url_match = $magic_url_replace = array();
      // Be sure to not let the matches cross over. ;)

       // relative urls for this board
      $magic_url_match[] = '#(^|[\n\t (])(' . preg_quote($server_url, '#') . ')/(' . get_preg_expression('relative_url_inline') . ')#ie';
      $magic_url_replace[] = "'\$1<!-- l --><a class=\"postlink\" href=\"\$2/' . preg_replace('/(&amp;|\?)sid=[0-9a-f]{32}/', '\\\\1', '\$3') . '\">' . preg_replace('/(&amp;|\?)sid=[0-9a-f]{32}/', '\\\\1', '\$3') . '</a><!-- l -->'";

      // matches a xxxx://aaaaa.bbb.cccc. ...
      $magic_url_match[] = '#(^|[\n\t (])(' . get_preg_expression('url_inline') . ')#ie';
      $magic_url_replace[] = "'\$1<!-- m --><a class=\"postlink\" href=\"\$2\">' . ((strlen('\$2') > 55) ? substr(str_replace('&amp;', '&', '\$2'), 0, 39) . ' ... ' . substr(str_replace('&amp;', '&', '\$2'), -10) : '\$2') . '</a><!-- m -->'";

      // matches a "www.xxxx.yyyy[/zzzz]" kinda lazy URL thing
      $magic_url_match[] = '#(^|[\n\t (])(' . get_preg_expression('www_url_inline') . ')#ie';
      $magic_url_replace[] = "'\$1<!-- w --><a class=\"postlink\" href=\"http://\$2\">' . ((strlen('\$2') > 55) ? substr(str_replace('&amp;', '&', '\$2'), 0, 39) . ' ... ' . substr(str_replace('&amp;', '&', '\$2'), -10) : '\$2') . '</a><!-- w -->'";

      // matches an email@domain type address at the start of a line, or after a space or after what might be a BBCode.
      $magic_url_match[] = '/(^|[\n\t )])(' . get_preg_expression('email') . ')/ie';
      $magic_url_replace[] = "'\$1<!-- e --><a class=\"postlink\" href=\"mailto:\$2\">' . ((strlen('\$2') > 55) ? substr('\$2', 0, 39) . ' ... ' . substr('\$2', -10) : '\$2') . '</a><!-- e -->'";
   }

   return preg_replace($magic_url_match, $magic_url_replace, $text);
}


Replace
Code: Select all
$magic_url_match = $magic_url_replace = array();
      // Be sure to not let the matches cross over. ;)

       // relative urls for this board
      $magic_url_match[] = '#(^|[\n\t (])(' . preg_quote($server_url, '#') . ')/(' . get_preg_expression('relative_url_inline') . ')#ie';
      $magic_url_replace[] = "'\$1<!-- l --><a class=\"postlink\" href=\"\$2/' . preg_replace('/(&amp;|\?)sid=[0-9a-f]{32}/', '\\\\1', '\$3') . '\">' . preg_replace('/(&amp;|\?)sid=[0-9a-f]{32}/', '\\\\1', '\$3') . '</a><!-- l -->'";

      // matches a xxxx://aaaaa.bbb.cccc. ...
      $magic_url_match[] = '#(^|[\n\t (])(' . get_preg_expression('url_inline') . ')#ie';
      $magic_url_replace[] = "'\$1<!-- m --><a class=\"postlink\" href=\"\$2\">' . ((strlen('\$2') > 100) ? substr(str_replace('&amp;', '&', '\$2'), 0, 100) . ' ... ' . substr(str_replace('&amp;', '&', '\$2'), -10) : '\$2') . '</a><!-- m -->'";

      // matches a "www.xxxx.yyyy[/zzzz]" kinda lazy URL thing
      $magic_url_match[] = '#(^|[\n\t (])(' . get_preg_expression('www_url_inline') . ')#ie';
      $magic_url_replace[] = "'\$1<!-- w --><a class=\"postlink\" href=\"http://\$2\">' . ((strlen('\$2') > 100) ? substr(str_replace('&amp;', '&', '\$2'), 0, 100) . ' ... ' . substr(str_replace('&amp;', '&', '\$2'), -10) : '\$2') . '</a><!-- w -->'";

      // matches an email@domain type address at the start of a line, or after a space or after what might be a BBCode.
      $magic_url_match[] = '/(^|[\n\t )])(' . get_preg_expression('email') . ')/ie';
      $magic_url_replace[] = "'\$1<!-- e --><a class=\"postlink\" href=\"mailto:\$2\">' . ((strlen('\$2') > 100) ? substr('\$2', 0, 100) . ' ... ' . substr('\$2', -10) : '\$2') . '</a><!-- e -->'";
   }

   return preg_replace($magic_url_match, $magic_url_replace, $text);
}


This allows url's up to 100 characters long to be displayed in full
installspark
Crewman
Crewman
 
Posts: 1
Joined: 15 Jul 2007, 08:41
Gender: Male


Re: Fix for long url's and link opens in new window

Postby stitch626 » 15 Jul 2007, 09:13

Thanks for this code installspark but this should have been posted in the Tutorials forum.

I will move this topic to the proper forum.
User avatar
stitch626    
STG Moderator Leader
STG Moderator Leader
 
Posts: 3185
Joined: 08 Feb 2007, 20:47
Location: Michigan
Favorite Team: Detroit Red Wings
Gender: Male
phpBB Knowledge: 7

Re: [HOW TO] Fix for long url's and link opens in new window

Postby eFantasy » 15 Jul 2007, 11:07

Nice! :good:
Dark Blitz - A graphics community
Styles: proFantasy
New ranks: Blue Blitz ranks
Topic icons: More prosilver topic icons
Image
User avatar
eFantasy    
Style Author
Style Author
 
Posts: 528
Joined: 26 Apr 2007, 09:53
Location: Netherlands
Gender: Male
phpBB Knowledge: 5


Return to Tutorials and How-Tos

Who is online

Users browsing this forum: No registered users and 3 guests