You are here

function _shortener_filter_url in Shorten URLs 7

Same name and namespace in other branches
  1. 6 shortener/shortener.module \_shortener_filter_url()
  2. 7.2 shortener/shortener.module \_shortener_filter_url()

Replaces URLs with the shortened version.

Unlike the core URL filter, this only matches HTTP and HTTPS addresses, because those are the only ones accepted by most URL shortening services.

1 string reference to '_shortener_filter_url'
shortener_filter_info in shortener/shortener.module
Implements hook_filter_info().

File

shortener/shortener.module, line 43
Provides an input filter that replaces URLs with a shortened version.

Code

function _shortener_filter_url($text, $filter, $format) {
  $format = $format->format;
  $length = $filter->settings['shortener_url_length'];

  // Pass length to regexp callback.
  _filter_url_trim('', $length);

  // Pass behavior to regexp callback.
  _shortener_url_behavior(NULL, FALSE, $filter->settings['shortener_url_behavior'], $length);
  $text = ' ' . $text . ' ';

  // Match absolute URLs.
  $text = preg_replace_callback("`(<p>|<li>|<br\\s*/?>|[ \n\r\t\\(])((http://|https://)([a-zA-Z0-9@:%_+*~#?&=.,/;-]*[a-zA-Z0-9@:%_+*~#&=/;-]))([.,?!]*?)(?=(</p>|</li>|<br\\s*/?>|[ \n\r\t\\)]))`i", '_shortener_url_behavior', $text);

  // Match www domains/addresses.
  $text = preg_replace_callback("`(<p>|<li>|[ \n\r\t\\(])(www\\.[a-zA-Z0-9@:%_+*~#?&=.,/;-]*[a-zA-Z0-9@:%_+~#\\&=/;-])([.,?!]*?)(?=(</p>|</li>|<br\\s*/?>|[ \n\r\t\\)]))`i", '_shortener_url_parse_partial_links', $text);
  $text = substr($text, 1, -1);
  return $text;
}