You are here

function elf_replace in External Links Filter 5.2

Same name and namespace in other branches
  1. 5.3 elf.module \elf_replace()
  2. 5 elf.module \elf_replace()
  3. 6.3 elf.module \elf_replace()
  4. 6.2 elf.module \elf_replace()
  5. 7.3 elf.module \elf_replace()
1 call 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, NULL, NULL, TRUE);

  // if the link is external (e.g., it starts with http) and it's not an absolute link to the current site
  if (strpos($match[1], 'http') === 0 && strpos($match[1], $site_url) === FALSE) {

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

      // it is faster to use PHP's string functions then complex regexes
      // in this case we strip off the last > in the <a> and then append our class and close the tag
      $link = substr($match[0], 0, -1);
      $link .= ' class="external-link">';
    }
    else {

      // append external class if this <a> already has a class
      $link = preg_replace('!class="([^"]+)"!', 'class="${1} external-link"', $match[0]);
    }
  }
  else {
    if (strpos($match[1], 'mailto:') === 0) {

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

        // it is faster to use PHP's string functions then complex regexes
        // in this case we strip off the last > in the <a> and then append our class and close the tag
        $link = substr($match[0], 0, -1);
        $link .= ' class="mailto-link">';
      }
      else {

        // append external class if this <a> already has a class
        $link = preg_replace('!class="([^"]+)"!', 'class="${1} mailto-link"', $match[0]);
      }
    }
  }
  return $link;
}