function word_link_convert_text_recursively in Word Link 7.2
Helper function for converting text.
Parameters
string $text: Input text.
string $pattern: Regular expression pattern.
array $words: Array of all words.
string $disallowed: Disallowed tags.
array $settings: Array of filter settings.
string $tag: Tag that will be used to replace word.
1 call to word_link_convert_text_recursively()
- word_link_convert_text in ./
word_link.module - Find and convert defined word to link.
File
- ./
word_link.module, line 387
Code
function word_link_convert_text_recursively(&$text, $pattern, $words, $disallowed, $settings, $tag) {
// Create DOM object.
$dom = filter_dom_load($text);
$xpath = new DOMXPath($dom);
$text_nodes = $xpath
->query('//text()[not(ancestor::a) ' . $disallowed . ']');
foreach ($text_nodes as $original_node) {
$text = $original_node->nodeValue;
$match_count = preg_match_all($pattern, $text, $matches, PREG_OFFSET_CAPTURE);
if ($match_count > 0) {
$offset = 0;
$parent = $original_node->parentNode;
$next = $original_node->nextSibling;
$parent
->removeChild($original_node);
foreach ($matches[0] as $delta => $match) {
$match_text = $match[0];
$match_pos = $match[1];
$text_lower = drupal_strtolower($match_text);
$word = $words[$text_lower];
$word_cache_id = $word->id . '_' . md5($match_text);
if ($word->case_sensitive && $word->text == $match_text || !$word->case_sensitive) {
$prefix = substr($text, $offset, $match_pos - $offset);
$parent
->insertBefore($dom
->createTextNode($prefix), $next);
$link = $dom
->createDocumentFragment();
$word_link_rendered =& drupal_static('word_link_rendered');
if (!isset($word_link_rendered[$word_cache_id])) {
if ($cache = cache_get('word_link_rendered_' . $word_cache_id)) {
$word_link_rendered[$word_cache_id] = $cache->data;
}
else {
$target = url_is_external($word->url) ? '_blank' : '';
$url_external = url_is_external($word->url);
$url_options = array();
$url_path = NULL;
if ($url_external) {
$url_path = $word->url;
}
else {
$url_parts = parse_url($word->url);
$url_query = array();
if (isset($url_parts['query'])) {
parse_str($url_parts['query'], $url_query);
}
$url_options = array(
'query' => $url_query,
'fragment' => isset($url_parts['fragment']) ? $url_parts['fragment'] : '',
);
if (empty($url_parts['path'])) {
// Assuming that URL starts with #.
$url_options['external'] = TRUE;
$url_path = NULL;
}
else {
$url_path = $url_parts['path'];
}
}
$attributes = array(
'href' => url($url_path, $url_options),
'title' => $word->url_title,
'class' => $word->class,
'target' => $target,
'rel' => $word->rel,
);
if ($settings['word_link_highlight']) {
$tag = 'span';
unset($attributes['href'], $attributes['target'], $attributes['rel']);
}
$word_link_rendered[$word_cache_id] = theme('word_link', array(
'text' => $match_text,
'tag' => $tag,
'attributes' => array_filter($attributes),
));
if (!empty($settings['word_link_wrap_tag'])) {
$word_link_rendered[$word_cache_id] = theme('html_tag', array(
'element' => array(
'#tag' => $settings['word_link_wrap_tag'],
'#value' => $word_link_rendered[$word_cache_id],
),
));
}
cache_set('word_link_rendered_' . $word_cache_id, $word_link_rendered[$word_cache_id], 'cache');
}
}
$link
->appendXML($word_link_rendered[$word_cache_id]);
$parent
->insertBefore($link, $next);
$offset = $match_pos + strlen($match_text);
}
else {
$prefix = substr($text, $offset, $match_pos - $offset);
$parent
->insertBefore($dom
->createTextNode($prefix), $next);
$parent
->insertBefore($dom
->createTextNode($match_text), $next);
$offset = $match_pos + strlen($match_text);
}
if ($delta == $match_count - 1) {
$suffix = substr($text, $offset);
$parent
->insertBefore($dom
->createTextNode($suffix), $next);
}
}
}
}
$text = filter_dom_serialize($dom);
}