You are here

function _autofloat_filter in AutoFloat 7

Same name and namespace in other branches
  1. 8 autofloat.module \_autofloat_filter()
  2. 7.2 autofloat.module \_autofloat_filter()

Find a rejected 'div', a selected 'span', a link with image or an image.

1 string reference to '_autofloat_filter'
autofloat_filter_info in ./autofloat.module
Implements hook_filter_info().

File

./autofloat.module, line 84
Autofloat module: A filter that floats images left and right automatically.

Code

function _autofloat_filter($text, $filter) {
  $selector = variable_get('autofloat_span', 'caption,flickr-wrap');

  // Divide the variable into more selectors if a comma is found.
  $selector = explode(',', $selector);

  // Make sure a variable exists, if not the regex breaks.
  if (isset($selector[0]) == FALSE) {
    $select = '';
  }
  else {

    // Set the first selector.
    $select = trim($selector[0]);
  }
  foreach (array_slice($selector, 1) as $value) {

    // Concatenate with subsequent selectors, divided with OR symbol.
    $select .= '|' . trim($value);
  }
  $rejector = variable_get('autofloat_div', 'flickr-photoset');

  // Divide the variable into more rejectors if a comma is found.
  $rejector = explode(',', $rejector);

  // Make sure a variable exists, if not the regex breaks.
  if (isset($rejector[0]) == FALSE) {
    $reject = '';
  }
  else {

    // Set the first rejector.
    $reject = trim($rejector[0]);
  }
  foreach (array_slice($rejector, 1) as $value) {

    // Concatenate with subsequent rejectors, divided with OR symbol.
    $reject .= '|' . trim($value);
  }

  // Regex in four parts (divided by '|') matching a rejected 'div', a
  // selected 'span', a link with image or an image. A pattern inside an
  // already found pattern gets ignored, to avoid floats inside floats.
  $text = preg_replace_callback('@<(
  div ([^>]*)(class\\s*=\\s*["\'](nofloat|' . $reject . ')["\'])([^>]*)>(.*?)</div| # find a rejected div
  span([^>]*)(class\\s*=\\s*["\'](float|' . $select . ')["\'])([^>]*)>(.*?)</span| # find a selected span
  (a[^>]*)(href=)([^>]*)(><img[^>]*>(.*?)(</img>)?</a)| # find a link with image
  img (.+?) # find an image
  )>@isx', 'autofloat_filter_callback', $text);
  return $text;
}