Hello all,
I'm an older coder, but relatively new at vB/PHP hacks, so am running into odd problems that I'm not certain of. Any assistance greatly received!
Here is my current situation: I've created a hack that runs in postbit, and uses regex to find phrases in a post which, if matched with terms entered in a vb-options setting of dictionary terms, hyperlinks them and links them to a dictionary file.
All is working well, save for one problem: the regex code finds terms that are not internal to words (e.g. finding 'bin' on its own, but
not in the middle of 'albino' by looking for terms followed by a space or a non-letter character. Here is the regex (which presumes a number of terms are already in a variable called $terms_regex, e.g. 'bin|trash|dog'):
Code:
$terms_regex='/(?:('.$terms_regex.')([\s$_]|[^a-rt-z0-9]))/';
As such, that works fine. However, by determining whole words by finding trailing spaces or non-letters (e.g. commas, full stops, etc.), it includes these in the actual terms - so it matches "bin " or "bin," and not simply the word itself.
Here is the code that texts the regex and replaces located terms with linked versions:
Quote:
$terms_regex='/(?'.$terms_regex.')([\s$_]|[^a-rt-z0-9]))/';
$reference = '\\0';
if ( $vbulletin->options['mdict_newwin'] == 1 ) {
// $link = '<a target="_blank" class="dictlink" href="' . $vbulletin->options['mdict_dictloc'] .'#'. $reference. '">'.$reference.'</a>';
$link = '<a target="_blank" class="dictlink" href="' . $vbulletin->options['mdict_dictloc'] .'#'. $reference. '">'.$reference.'</a>';
} else {
// $link = '<a class="dictlink" href="' . $vbulletin->options['mdict_dictloc'] .'#'. $reference. '">'.$reference.'</a>';
$link = '<a class="dictlink" href="' . $vbulletin->options['mdict_dictloc'] .'#'. $reference. '">'.$reference.'</a>';
}
$text=preg_replace($terms_regex,$link,$text);
|
Again, this works as such; but the linked terms include linking of the whole regex-located term, which includes the trailing space or punctuation. What I need is a way to trim off the last character of the expression when creating the link.
I've tried doing this by using rtrim() on $reference - but this doesn't work -- I think because the actual contents of that variable are a mystery to me. They are received from the line of code I've put in boldface, above (namely:
$reference = '\\0'. This is a snippet of code that I gleaned from looking at other vB hacks; but its actual nature is a mystery to me. How precisely does it work? What kind of variable is it producing? If I have the script echo it onto the page, it correctly displays the word (e.g. 'bin'); but if I then do an if..then... on the variable (e.g. "if ($reference == 'bin')"), it fails (!).
I would appreciate - greatly - any assistance, as I admit I'm rather stumped.
Many thanks!
NB: For fuller reference, below is the fuller code of the hack I'm working on, so you can see where the above code snippets lie and how they're being called:
Quote:
<plugin active="1" executionorder="5">
<title>Convert to Monachos Dictionary Link</title>
<hookname>global_start</hookname>
<phpcode><![CDATA[
function makedict ($text = ''){
// Skip everything within a hyperlink, a <pre> block, a <code> block, or a tag
// We skip tags because something like <img src="blah" alt="John 3:16"> should not be messed with
$anchor_regex = '<a\s+href.*?<\/a>';
$pre_regex = '<pre>.*<\/pre>';
$code_regex = '<code>.*<\/code>';
$tag_regex = '<(?:[^<>\s]*)(?:\s[^<>]*){0,1}>'; // $tag_regex='<[^>]+>';
$split_regex = "/((?:$anchor_regex)|(?:$pre_regex)|(?:$code_regex)| (?:$tag_regex))/i";
$parsed_text = preg_split($split_regex,$text,-1,PREG_SPLIT_DELIM_CAPTURE);
$linked_text = '';
while (list($key,$value) = each($parsed_text)) {
if (preg_match($split_regex,$value)) {
$linked_text .= $value; // if it is an HTML element or within a link, just leave it as is
} else {
$linked_text .= makedictAddLinks($value); // if it's text, parse it for Monachos Dictionary linking
}
}
return $linked_text;
}
function makedictAddLinks($text='') {
global $vbulletin;
if ( $vbulletin->options['mdict_terms1'] != '' ) {
$terms_regex = $vbulletin->options['mdict_terms1'];
} else {
$terms_regex = 'Monachos.net';
}
if ( $vbulletin->options['mdict_terms2'] != '' ) {
$terms_regex .= '|' . $vbulletin->options['mdict_terms2'];
}
if ( $vbulletin->options['mdict_terms3'] != '' ) {
$terms_regex .= '|' . $vbulletin->options['mdict_terms3'];
}
$terms_regex='/(?'.$terms_regex.')([\s$_]|[^a-rt-z0-9]))/';
$reference = '\\0';
if ( $vbulletin->options['mdict_newwin'] == 1 ) {
// $link = '<a target="_blank" class="dictlink" href="' . $vbulletin->options['mdict_dictloc'] .'#'. $reference. '">'.$reference.'</a>';
$link = '<a target="_blank" class="dictlink" href="' . $vbulletin->options['mdict_dictloc'] .'#'. $reference. '">'.$reference.'</a>';
} else {
// $link = '<a class="dictlink" href="' . $vbulletin->options['mdict_dictloc'] .'#'. $reference. '">'.$reference.'</a>';
$link = '<a class="dictlink" href="' . $vbulletin->options['mdict_dictloc'] .'#'. $reference. '">'.$reference.'</a>';
}
$text=preg_replace($terms_regex,$link,$text);
return $text;
}
]]></phpcode>
</plugin>
<plugin active="1" executionorder="5">
<title>Convert to Monachos Dictionary Link</title>
<hookname>postbit_display_complete</hookname>
<phpcode><![CDATA[
$post['message']= makedict($post['message']);
]]></phpcode>
</plugin>
|