Hello,
Here is my situation: with my vbulletin board, i'm using a crappy glossary script, but i really like it so i would like to add an additional feature that would be really useful to me since i have a couple hundreds of terms in my database.
Basically, the script looks in the database for listed terms, and link them to the glossary definition page.
Currently, the script will only do 1 replacement for each terms. But i would also like to replace the terms located inside $glossaryfield6 (the terms to replace are separated by a comma)
Exemple:
$glossaryname = "animals";
$glossaryfield6 = "cat, dog, fish, cow, horse, rabbit";
The script will be doing replacements on the following string:
"I like animals : I have a dog and a cat."
Currently the script will only replace the word "animals", but i also want to replace all words from $glossaryfield6
I spent a couple of hours trying to do this with my poor php skills, but i always get errors or incorrect replacements.
Here is the main part of the script:
<?php
error_reporting(E_ALL & ~E_NOTICE);
define('NO_REGISTER_GLOBALS', 1);
require_once('./includes/functions.php');
function fetch_glossary_autolinker($glossaryterm)
{
global $vbulletin, $vbphrase;
static $crosslinkglossary = false;
static $findotherterms = array();
static $replacelinkterms = array();
if (!$crosslinkglossary)
{
$termlinks = $vbulletin->db->query(" SELECT glossaryname, glossarydescription, glossaryid FROM " . TABLE_PREFIX . "glossary WHERE glossaryvalid = 1");
while($foundterms = $vbulletin->db->fetch_array($termlinks))
{
$glossaryname = $foundterms['glossaryname'];
$glossaryid = $foundterms['glossaryid'];
$glossarydescription = $foundterms['glossarydescription'];
/*
The additional terms i want to replace are located inside $glossaryfield6.
The content of this variable looks like something like this :
"term1, term2, term3, term4, term5"
*/
$glossaryfield6 = $foundterms['glossaryfield6'];
$glossarypath = $vbulletin->options['bburl'];
/*
This is the words that will be replaced. Currently, the script only replace one word, i need to find how to ALSO replace all the terms listed in "$glossaryfield6" and separated by a comma
*/
$findotherterms[] = "#\b$glossaryname\b(?=\s|[.,?!;:]\s)#i";
if ($vbulletin->options['vbglossary_crosslinking_image'] == 1)
{
/*
This is the result of the replacement. The terms found will be replaced by a link.
Basically i don't want to modify anything, because the additional terms that needs replacement should be replaced by the same thing.
*/
$replacelinkterms[] = "<span class=\"glossarycrosslinkimage\"><a href=\"$glossarypath/glossary.php?do=viewglossary&term=$glossaryid\"' onmouseover=\"glossary_ajax_showTooltip('$glossarypath/glossary_crosslinking.php?do=crosslink&term=$glossaryid',this,true);return false\" onmouseout=\"glossary_ajax_hideTooltip()\"><b>$glossaryname </b></a></span>";
}
else
{
$replacelinkterms[] = "<span class=\"glossarycrosslink\"><a href=\"$glossarypath/glossary.php?do=viewglossary&term=$glossaryid\"' onmouseover=\"glossary_ajax_showTooltip('$glossarypath/glossary_crosslinking.php?do=crosslink&term=$glossaryid',this,true);return false\" onmouseout=\"glossary_ajax_hideTooltip()\"><b>$glossaryname</b></a></span>";
}
}
$crosslinkglossary = true;
}
$glossaryterm = preg_replace($findotherterms, $replacelinkterms, $glossaryterm, $vbulletin->options['vbglossary_crosslinking_limit']);
return $glossaryterm;
}
// ===========================================================================================
// Construct custom glossary fields. (Edit by: Lucas / Version Add: 1.2.1)
// ===========================================================================================
function construct_custom_glossaryfields($basename, $customglossaryfield, $row)
{
global $vbphrase;
$data = explode("\n", $customglossaryfield['glossaryfielddata']);
$fieldname = 'glossaryfield' . $customglossaryfield['glossaryfieldid'];
$customglossaryfieldname = $basename . '[glossaryfield' . $customglossaryfield['glossaryfieldid'] . ']';
if (!is_array($row))
{
$row = array($fieldname => '');
}
if ($customglossaryfield['glossaryfieldtype'] == 'select')
{
foreach ($data AS $key => $val)
{
$key++;
$selectoutput = '';
$selected = '';
$val = trim($val);
if (!$row["$fieldname"] AND $key == 0 AND $customglossaryfield['glossaryfielddef'] == 0)
{
$selected = '';
}
else if (!$row["$fieldname"] AND $key == 0 AND $customglossaryfield['glossaryfielddef'] == 1)
{
$selected = 'selected="selected"';
}
else if (!$row["$fieldname"] AND $key == 1 AND $customglossaryfield['glossaryfielddef'] == 2)
{
$selected = 'selected="selected"';
}
else if ($val == $row["$fieldname"])
{
$selected = 'selected="selected"';
}
$selectbits .= "<option value=\"$val\" $selected>$val</option>";
}
$selectoutput = "<select name=\"$customglossaryfieldname\" tabindex=\"1\" class=\"bginput\">
<option value=\"0\">$vbphrase[please_select_one]</option>
$selectbits
</select>";
}
else if ($customglossaryfield['glossaryfieldtype'] == 'radio')
{
foreach ($data AS $key => $val)
{
$key++;
$checked = '';
$val = trim($val);
if (!$row["$fieldname"] AND $key == 1 AND $customglossaryfield['glossaryfielddef'] == 1)
{
$checked = 'checked="checked"';
}
else if ($val == $row["$fieldname"])
{
$checked = 'checked="checked"';
}
$radiobits .= "<label for=\"rb_{$key}_$customglossaryfieldname\"><input type=\"radio\" name=\"$customglossaryfieldname\" value=\"$val\" id=\"rb_{$key}_$customglossaryfieldname\" tabindex=\"1\" $checked>$val</label>";
$perline++;
if ($customglossaryfield['glossaryfieldperline'] > 0 AND $perline >= $customglossaryfield['glossaryfieldperline'])
{
$radiobits .= '<br />';
$perline = 0;
}
}
}
else if ($customglossaryfield['glossaryfieldtype'] == 'checkbox')
{
$checkboxbits = '';
$perline = 0;
$checkboxdata = explode(", ", $row["$fieldname"]);
$checkboxinformation = array();
foreach ($checkboxdata AS $checkboxkey => $checkboxvalue)
{
$checkboxinformation[] = htmlspecialchars($checkboxvalue);
}
foreach ($data AS $key => $val)
{
$val = trim($val);
$checkboxbits .= "<label for=\"cb_{$key}_$customglossaryfieldname\"><input type=\"checkbox\" name=\"{$customglossaryfieldname}[]\" value=\"$val\" id=\"cb_{$key}_$customglossaryfieldname\" tabindex=\"1\" " . iif(in_array($val, $checkboxinformation), 'checked=\"checked\"') . ">$val</label> ";
$perline++;
if ($customglossaryfield['glossaryfieldperline'] > 0 AND $perline >= $customglossaryfield['glossaryfieldperline'])
{
$checkboxbits .= '<br />';
$perline = 0;
}
}
}
else if ($customglossaryfield['glossaryfieldtype'] == 'select_multiple')
{
$selectmultioutput = '';
$selectbits = '';
$selectdata = explode(", ", $row["$fieldname"]);
$selectinformation = array();
foreach ($selectdata AS $selectkey => $selectvalue)
{
$selectinformation[] = htmlspecialchars($selectvalue);
}
foreach ($data AS $key => $val)
{
$selectbits .= "<option value=\"$val\" " . iif(in_array($val, $selectinformation), 'selected=\"selected\"') . ">$val</option>";
}
$selectmultioutput = "<select name=\"{$customglossaryfieldname}[]\" multiple=\"multiple\" size=\"$customglossaryfield[glossaryfieldheight]\" tabindex=\"1\" class=\"bginput\">
$selectbits
</select>";
}
eval('$displayresults .= "' . fetch_template('glossary_editadd_customfields') . '";');
return $displayresults;
}
// ===========================================================================================
// Alternating Row Colors. (Edit by: Lucas / Version Add: 1.2.1)
// ===========================================================================================
function altrowcolors($altrowcount)
{
return $result = ($altrowcount %2 == 0)? ('alt1') : ('alt2');
}
// ===========================================================================================
// Trim glossary results. (Edit by: Lucas / Version Add: 1.2.2)
// ===========================================================================================
function fetch_trimmed_glossary_term($title, $chars = -1)
{
global $vbulletin, $vbphrase;
if ($chars)
{
if (vbstrlen($title) > $chars)
{
$title = substr($title, 0, $chars);
if (($pos = strrpos($title, ' ')) !== false)
{
$title = substr($title, 0, $pos);
}
return $title . "<br><b>... $vbphrase[glossary_readterm]</b>";
}
else
{
return $title;
}
}
else
{
return $title;
}
}
?>
If possible, i would also like to only link the terms to the glossary page, but without changing the word. I prefer to keep the same word that was in the text rather than replacing it with the term located in the database (would fix some bugs with capital letters)
Thanks for helping me