function alinks_make_links in Alinks 7
Same name and namespace in other branches
- 6 alinks.module \alinks_make_links()
Transform the first instance of any word defined to links
1 call to alinks_make_links()
- alinks_node_view in ./
alinks.module - Implementation of hook_node_view().
File
- ./
alinks.module, line 162 - this module allows users to associates defined text to links
Code
function alinks_make_links($body, $words) {
if (is_array($words) && isset($body)) {
// create the replacement array
$url = drupal_lookup_path('alias', $_GET['q']) ? drupal_lookup_path('alias', $_GET['q']) : $_GET['q'];
$i = 0;
$alink_options = array();
$links_chars = array(
'/',
'-',
);
$links_chars_replace = array(
'alink_slash',
'alink_minus',
);
$replacement = array();
foreach ($words as $word) {
if ($word->alink_url != $url) {
$alink_start_boundary = $word->alink_start_boundary == 1 ? 'B' : 'b';
$alink_end_boundary = $word->alink_end_boundary == 1 ? 'B' : 'b';
$alink_case_insensivity = $word->alink_case_insensitive == 1 ? 'i' : '';
$word->alink_text = trim(htmlspecialchars($word->alink_text));
$alink_text[] = '$\\' . $alink_start_boundary . '(' . preg_quote($word->alink_text, '$') . ')\\' . $alink_end_boundary . '(?!((?!alink_replaced).)*alink_replaced</a>)$u' . $alink_case_insensivity;
if ($word->alink_external != 1) {
$alink_path = 'alink_check' . str_replace('/', 'alink_slash', $word->alink_url) . 'alink_check';
}
else {
$alink_path = str_replace($links_chars, $links_chars_replace, $word->alink_url) . 'alink_check';
$alink_options['absolute'] = TRUE;
}
if (!empty($word->alink_class)) {
$alink_class = 'alink_check' . str_replace(' ', 'alink_space', $word->alink_class) . 'alink_check';
$alink_options['attributes']['class'] = $alink_class;
}
if (!empty($word->url_title)) {
$alink_title = 'alink_check' . str_replace(' ', 'alink_space', check_plain($word->url_title)) . 'alink_check';
$alink_options['attributes']['title'] = $alink_title;
}
$alink_url[] = l('alink_replaced\\1alink_replaced', $alink_path, $alink_options);
$i++;
}
}
if ($i > 0) {
$alink_url = str_replace(array(
'&amp;',
'&lt;',
'&gt;',
), array(
'&',
'<',
'>',
), $alink_url);
// we replace new lines with a temporary delimiter
$carriage = array(
"\r\n",
"\n",
"\r",
);
$carriage_replacement = array(
" cariage_replacement_rn ",
" cariage_replacement_n ",
" cariage_replacement_r ",
);
$body = str_replace($carriage, $carriage_replacement, $body);
// we get out the already existing links
preg_match_all('/\\<a\\s.*?\\>(.*?)\\<\\/a\\>/i', $body, $linka);
// create the replacement array
foreach ($linka[0] as $key => $values) {
$replacement[] = ' alink_delimiter_' . $key . ' ';
}
// replace the links with the replacement text
$body = str_replace($linka[0], $replacement, $body);
// we get all the text that is not inside a html tag from the modified text
preg_match_all('/\\>(.*?)\\</', $body, $output);
$output[0] = array_unique($output[0]);
$output[1] = array_unique($output[1]);
// transform the result array to a string so we can use the limit argument
$text = implode(' alink_delimiter_one_string ', $output[1]);
$limit = variable_get('alinks_limit', 1);
// make the actual replacement
if ($limit == -1) {
$output[1] = preg_replace($alink_text, $alink_url, $text);
}
else {
$output[1] = preg_replace($alink_text, $alink_url, $text, $limit);
}
// rebuild the array
$output[1] = explode(' alink_delimiter_one_string ', $output[1]);
$our_output = array();
$i = 0;
// we make sure the text will pe replaced outside any tag
foreach ($output[1] as $key => $values) {
if (!$values) {
$our_output[$i] = '><';
}
else {
$our_output[$i] = str_replace($values, '>' . $values . '<', $values);
}
$i++;
}
// insert the new text in the full text
$body = str_replace($output[0], $our_output, $body);
// and put back the links in the text
$body = str_replace($replacement, $linka[0], $body);
$body = str_replace('alink_check', '', $body);
$body = str_replace('alink_replaced', '', $body);
$body = str_replace('alink_space', ' ', $body);
$body = str_replace($links_chars_replace, $links_chars, $body);
// and finaly put back the new lines
$body = str_replace($carriage_replacement, $carriage, $body);
}
}
return $body;
}