You are here

function elf_replace in External Links Filter 6.2

Same name and namespace in other branches
  1. 5.3 elf.module \elf_replace()
  2. 5 elf.module \elf_replace()
  3. 5.2 elf.module \elf_replace()
  4. 6.3 elf.module \elf_replace()
  5. 7.3 elf.module \elf_replace()
1 string reference to 'elf_replace'
elf_filter in ./elf.module
Implementation of hook_filter().

File

./elf.module, line 83

Code

function elf_replace($match) {
  $link = $match[0];
  $site_url = url(NULL, array(
    'absolute' => TRUE,
  ));

  // The link is external, but does not point to the site itself.
  if (strpos($match[1], 'http') === 0 && strpos($match[1], $site_url) === FALSE) {

    // There is no class yet.
    if (strpos($match[0], 'class="') === FALSE) {

      // It is faster to use PHP's string functions than complex regexes. In
      // this case we strip off the greater than sign, add our class and
      // close the tag.
      $link = substr($match[0], 0, -1);
      $link .= ' class="external-link">';
    }
    else {

      // The class attribute already has a value. Append our own to it.
      $link = preg_replace('!class="([^"]+)"!', 'class="${1} external-link"', $match[0]);
    }
  }
  else {
    if (strpos($match[1], 'mailto:') === 0) {

      // There is no class yet.
      if (strpos($match[0], 'class="') === FALSE) {

        // It is faster to use PHP's string functions than complex regexes. In
        // this case we strip off the greater than sign, add our class and
        // close the tag.
        $link = substr($match[0], 0, -1);
        $link .= ' class="mailto-link">';
      }
      else {

        // The class attribute already has a value. Append our own to it.
        $link = preg_replace('!class="([^"]+)"!', 'class="${1} mailto-link"', $match[0]);
      }
    }
  }
  return $link;
}