function elf_replace in External Links Filter 5.3
Same name and namespace in other branches
- 5 elf.module \elf_replace()
- 5.2 elf.module \elf_replace()
- 6.3 elf.module \elf_replace()
- 6.2 elf.module \elf_replace()
- 7.3 elf.module \elf_replace()
Add classes to external or mailto links in a text.
Parameters
$text string: The text to filter.
Return value
string The (filtered) text.
1 call to elf_replace()
- elf_filter in ./
elf.module - Implementation of hook_filter().
File
- ./
elf.module, line 113
Code
function elf_replace($text, $format = NULL) {
$doc = new DOMDocument();
// We are using a wrapper to prevent tags from being added automatically.
$text = '<div>' . $text . '</div>';
$doc
->loadHTML($text);
$as = $doc
->getElementsByTagName('a');
foreach ($as as $a) {
$href = $a
->getAttribute('href');
// The link is absolute, and does not point to the site itself.
if (elf_url_external($href)) {
$class = ' elf-external';
$class .= $a
->getElementsByTagName('img')->length > 0 ? ' elf-img' : NULL;
$a
->setAttribute('class', $a
->getAttribute('class') . $class);
$nofollow = $format ? variable_get("elf_nofollow_{$format}", FALSE) : FALSE;
if ($nofollow) {
$a
->setAttribute('rel', $a
->getAttribute('rel') . ' nofollow');
}
}
else {
if (strpos($href, 'mailto:') === 0) {
$a
->setAttribute('class', $a
->getAttribute('class') . ' elf-mailto');
}
}
}
return substr($doc
->saveXML($doc
->getElementsByTagName('div')
->item(0)), 5, -6);
}